Merge Followed Accounts' Posts Into a Ten Item News Feed
Last updated: November 29, 2026
Quick Overview
Implement post, follow, unfollow, and get_feed, where get_feed returns the 10 most recent post IDs from the user and the accounts they follow, newest first.
Meta
November 29, 20260
0
0 solved
Implement post, follow, unfollow, and get_feed, where get_feed returns the 10 most recent post IDs from the user and the accounts they follow, newest first.
A classic Meta onsite object design plus algorithm question that echoes how News Feed fans in from followed accounts. The interviewer looks for a heap-based k-way merge that touches only about 10 posts per followed account at most, rather than collecting and sorting everything.
What the Interviewer Expects
- Store per-user posts in time order
- Use a global monotonic clock for ordering
- Merge per-user lists with a max-heap seeded by each latest post
- Stop after 10 items
- Handle self-follow and unfollow of non-followed users
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How does this change when a user follows 5,000 accounts?
- Compare fan-out on write versus fan-out on read.
- Add pagination with a cursor.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
post(user, post_id): user publishes a post.follow(a, b),unfollow(a, b): a starts or stops following b; following yourself is a no-op.get_feed(user): up to 10 post IDs from user and fo...
High-Level Design
Data model:
posts[user]: list of(time, post_id)appended in time order, with time from a global counter so posts from different users are comparable.following[user]: set of followees.
`ge...