You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/design-twitter-feed.md
+12-8Lines changed: 12 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -491,10 +491,11 @@ Instead of combining **all** tweets and sorting them (which is slow), we only ne
491
491
492
492
We use a **min-heap** (priority queue) because:
493
493
494
-
- We push only the _latest tweet_ of each followee.
495
-
- Each tweet has a timestamp (`count`), where smaller means more recent.
496
-
- We repeatedly extract the most recent tweet and then push the _next_ tweet from that same user.
497
-
- This is similar to merging K sorted lists efficiently.
494
+
- We start by pushing the latest tweet from each followee.
495
+
- Smaller `count` means the tweet is more recent.
496
+
- When we pop a tweet, we push the next older tweet from that same user.
497
+
- We repeat this until we collect `10` tweets.
498
+
- One user can still contribute many tweets if they are the most recent.
498
499
499
500
This ensures:
500
501
@@ -518,6 +519,7 @@ This ensures:
518
519
- For each followee:
519
520
- Push their most recent tweet into a min-heap:
520
521
`[time, tweetId, followeeId, nextIndex]`
522
+
- This is only the starting state of the merge, not the final candidate set.
521
523
- While heap is not empty and result has < `10` tweets:
522
524
- Pop the most recent tweet
523
525
- Add tweetId to the result
@@ -1171,8 +1173,9 @@ The trick:
1171
1173
1172
1174
- When a user posts a tweet, append it with a decreasing timestamp (`count`) and keep only the last `10` tweets.
1173
1175
- When getting the news feed:
1174
-
- If the user follows many people (>= `10`), we first gather only the recent tweets that _could_ appear in the final `10`, using a max-heap limited to size `10`.
1175
-
- Otherwise, push the most recent tweet of each followee directly into a min-heap and expand like a K-sorted-list merge.
1176
+
- If the user follows many people (>= `10`), keep only the `10` followees with the newest latest tweet by using a max-heap of size `10`.
1177
+
- This is safe: if a followee's newest tweet is already too old, none of their older tweets can make the final `10`.
1178
+
- Otherwise, push the latest tweet from each followee into a min-heap and keep expanding from the same user after each pop.
1176
1179
- In both cases, we never process more than **`10` tweets per followee**, and never extract more than **`10` results**.
1177
1180
1178
1181
This makes the method very fast even when users post a lot of tweets.
@@ -1188,8 +1191,9 @@ This makes the method very fast even when users post a lot of tweets.
1188
1191
Steps:
1189
1192
- Ensure user follows themselves.
1190
1193
- If followees >= `10`:
1191
-
- Build a **max-heap** that stores only the top `10` most recent tweets across followees.
1192
-
- Convert it to a **min-heap** for final processing.
1194
+
- Build a **max-heap** that keeps only the `10` newest latest tweets from followees.
1195
+
- Move them into a **min-heap** for final processing.
1196
+
- After popping a tweet, push the next older tweet from that same followee.
1193
1197
- Else:
1194
1198
- Push the newest tweet from each followee into a min-heap.
0 commit comments