p103 — Design Twitter — Hints
-
State first.
tweets: dict[user, list[(time, tweet_id)]]appended in post order.follows: dict[user, set[user]]. A single monotonicself.timecounter incremented on eachpostTweet. That’s the whole skeleton. -
Don’t forget the user’s own tweets.
getNewsFeed(uid)merges[uid] + sorted(follows[uid]). The most common LC submission bug is omittinguiditself. -
Top-10 across K streams = heap merge, not sort-everything. Push
(neg_time, user, idx)for the most-recent tweet of each user (start at last index per list) into a max-heap (negate for Python’s min-heap). Pop 10; on each pop, push the next-older tweet from that same user’s list. -
setfor follows, notlist.unfollowandfollowmust be O(1). AlistmakesunfollowO(F) and re-followinsert duplicates. -
unfollow(uid, uid)is a no-op (per LC). Don’t crash. Andunfollowof someone you don’t follow is also a no-op. Useset.discard, notset.remove.
If stuck: see solution.py.