Skip to content
Decoder

Design Twitter

Medium

Problem

Build a tiny social feed as a class Twitter:

- post_tweet(user_id, tweet_id) (postTweet): the user posts a tweet with a unique id. - get_news_feed(user_id) (getNewsFeed): return the ids of the 10 most recent tweets posted by the user or anyone they follow, newest first. - follow(follower_id, followee_id): the follower starts following the followee. - unfollow(follower_id, followee_id): the follower stops following them.

Users always see their own tweets. Following yourself, unfollowing someone you do not follow, and following someone twice should all be harmless.

Example 1

t = Twitter()

Example 2

t.post_tweet(1, 5)

Example 3

Input
t.get_news_feed(1)
Output
[5]

Example 4

t.follow(1, 2)

Example 5

t.post_tweet(2, 6)

Example 6

Input
t.get_news_feed(1)
Output
[6, 5]

Example 7

t.unfollow(1, 2)

Example 8

Input
t.get_news_feed(1)
Output
[5]

Constraints

  • 1 <= user_id, follower_id, followee_id <= 500
  • 0 <= tweet_id <= 10^4, and every tweet id is unique.
  • At most 3 * 10^4 calls are made in total.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.