p103 — Design Twitter — Hints

  1. State first. tweets: dict[user, list[(time, tweet_id)]] appended in post order. follows: dict[user, set[user]]. A single monotonic self.time counter incremented on each postTweet. That’s the whole skeleton.

  2. Don’t forget the user’s own tweets. getNewsFeed(uid) merges [uid] + sorted(follows[uid]). The most common LC submission bug is omitting uid itself.

  3. 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.

  4. set for follows, not list. unfollow and follow must be O(1). A list makes unfollow O(F) and re-follow insert duplicates.

  5. unfollow(uid, uid) is a no-op (per LC). Don’t crash. And unfollow of someone you don’t follow is also a no-op. Use set.discard, not set.remove.

If stuck: see solution.py.