Question presented to candidate:
"Design a simplified Twitter in Python with post_tweet(user, tweet_id), get_news_feed(user) returning the 10 most recent tweet ids from the user and everyone they follow, newest first, plus follow and unfollow. Then tell me how you would change the design if some users had millions of followers."
What a strong answer should cover:
- Hash maps: user โ set of followees, user โ list of
(time, tweet_id); a global counter as the timestamp. - The feed as a k-way merge of sorted lists with a max-heap, stopping after 10: O(f + 10 log f).
- Fan-out on read vs fan-out on write, the celebrity problem, and the hybrid approach.
- Edge cases: own tweets included, self-follow ignored, unfollowing a stranger is a no-op, empty feeds.
- Python details:
heapqis a min-heap, so push-time;collections.defaultdictcreates entries on read unless you use.get();set.discardvsset.remove.
Clarifying questions expected:
- Does the feed include the user's own tweets, and how many tweets?
- How skewed are follower counts, and what is the read/write ratio?
Code / implementation expected: Yes โ a Twitter class with dicts, sets and heapq, in Python 3.