Problemfollow based recent post feed

Session: Sign in to solve

Solution.txt

Follow-Based Recent Post Feed

Problem

Design FollowBasedRecentPostFeed, an in-memory social feed that stores posts and directed follow relationships, then derives each user's current recent feed.

Requirements

  • Upload a post. uploadPost adds the fresh postId as an active post owned by userId; it is newer than every earlier upload.
  • Delete a post. deletePost changes the specified active post owned by userId to deleted.
  • Follow an account. follow adds a directed relationship from followerId to followeeId.
  • Unfollow an account. unfollow removes that directed relationship.
  • Select feed posts. browseFeed considers exactly the current non-deleted posts owned by userId and by accounts that userId currently follows.
  • Order by upload. Eligible posts appear from newest to oldest according to successful upload order.
  • Limit the result. browseFeed returns the first 10 eligible post IDs, or all eligible post IDs when fewer than 10 exist.

API

SignatureReturnsBehavior
FollowBasedRecentPostFeed()Not applicableCreates an empty feed with no posts or follow relationships.
uploadPost(userId: string, postId: string)voidStores one fresh active post for the user.
deletePost(userId: string, postId: string)voidDeletes one active post owned by the user.
follow(followerId: string, followeeId: string)voidAdds one directed follow relationship.
unfollow(followerId: string, followeeId: string)voidRemoves one directed follow relationship.
browseFeed(userId: string)string[]Returns up to 10 eligible post IDs from newest to oldest.

Examples

StepOperationResult
1uploadPost("alice", "a1")void
2uploadPost("bob", "b1")void
3follow("alice", "bob")void
4browseFeed("alice")["b1", "a1"]
5deletePost("bob", "b1")void
6browseFeed("alice")["a1"]
7unfollow("alice", "bob")void

Constraints

  • User IDs and post IDs contain 1 to 32 ASCII characters. The first character is a letter; each remaining character is a letter, digit, underscore, or hyphen.
  • At most 1,000 distinct users, 10,000 posts, and 20,000 public method calls occur per testcase.
  • Every postId supplied to uploadPost is globally fresh and is never reused.
  • Every deletePost names an active post owned by the supplied userId, and each post is deleted at most once.
  • Every follow uses distinct users whose directed relationship is absent.
  • Every unfollow names a directed relationship that is present.
  • browseFeed may name a valid user with no posts or relationships.
  • Inputs satisfy these constraints.

Notes

Calls are sequential. Invalid calls, timestamps, visibility rules, ranking beyond upload order, pagination, concurrency, and persistence are outside the contract.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.