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.
uploadPostadds the freshpostIdas an active post owned byuserId; it is newer than every earlier upload. - Delete a post.
deletePostchanges the specified active post owned byuserIdto deleted. - Follow an account.
followadds a directed relationship fromfollowerIdtofolloweeId. - Unfollow an account.
unfollowremoves that directed relationship. - Select feed posts.
browseFeedconsiders exactly the current non-deleted posts owned byuserIdand by accounts thatuserIdcurrently follows. - Order by upload. Eligible posts appear from newest to oldest according to successful upload order.
- Limit the result.
browseFeedreturns the first 10 eligible post IDs, or all eligible post IDs when fewer than 10 exist.
API
| Signature | Returns | Behavior |
|---|---|---|
FollowBasedRecentPostFeed() | Not applicable | Creates an empty feed with no posts or follow relationships. |
uploadPost(userId: string, postId: string) | void | Stores one fresh active post for the user. |
deletePost(userId: string, postId: string) | void | Deletes one active post owned by the user. |
follow(followerId: string, followeeId: string) | void | Adds one directed follow relationship. |
unfollow(followerId: string, followeeId: string) | void | Removes one directed follow relationship. |
browseFeed(userId: string) | string[] | Returns up to 10 eligible post IDs from newest to oldest. |
Examples
| Step | Operation | Result |
|---|---|---|
| 1 | uploadPost("alice", "a1") | void |
| 2 | uploadPost("bob", "b1") | void |
| 3 | follow("alice", "bob") | void |
| 4 | browseFeed("alice") | ["b1", "a1"] |
| 5 | deletePost("bob", "b1") | void |
| 6 | browseFeed("alice") | ["a1"] |
| 7 | unfollow("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
postIdsupplied touploadPostis globally fresh and is never reused. - Every
deletePostnames an active post owned by the supplieduserId, and each post is deleted at most once. - Every
followuses distinct users whose directed relationship is absent. - Every
unfollownames a directed relationship that is present. browseFeedmay 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.