mediumHeap / Priority QueueTwitterFacebookAmazon
Streaming Timeline Hub
A micro-blogging backend fans in tweets from a user and everyone they follow, surfacing the 10 most recent in merged order.
Design a Twitter-like class supporting postTweet, getNewsFeed (top 10 recent tweets from user + followees), follow, unfollow.
Choose approach
post U1/T101
0feed U1
1U1↷U2
2post U2/T202
3feed U1
4U1⊘U2
5feed U1
6MapTimeline
#1→U1→T101
1/7
Narrationstep 1 of 7
User 1 posts tweet 101 at t=1.
Variables
user=1
tweet=101
t=1
How to think about it
- Monotonic timestamps give a total ordering across users without clock drift.
- Feed = multi-way merge across (self + followees) using a max-heap keyed by timestamp.
- Return at most 10 entries.
Edge cases to catch in interview
- User follows themselves -> treated once.
- Feed before any posts -> empty.
Examples
Example 1
[5]{"ops":[{"op":"post","user":1,"tweet":5},{"op":"feed","user":1}],"values":["post U1/T5","feed U1"]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed