Tap here to switch tabs
Problem
Submissions

Problem: Design Twitter

Try to solve the Design Twitter problem.

Statement

Design a simplified version of a social media service like Twitter where users can post tweets, follow or unfollow other users, and view the 1010 most recent tweets in their news feed.

Implement the Twitter class:

  • Twitter() Initializes the Twitter object.

  • void postTweet(int userId, int tweetId) Composes a new tweet with ID tweetId published by the user userId. Each call to this function is made with a unique tweetId.

  • List[int] getNewsFeed(int userId) Retrieves the 1010 most recent tweet IDs in the user’s news feed. Each item in the news feed must be posted by users whom the user follows or by the user themselves. Tweets must be ordered from most recent to least recent.

  • void follow(int followerId, int followeeId) The user with ID followerId starts following the user with ID followeeId.

  • void unfollow(int followerId, int followeeId) The user with ID followerId unfollows the user with ID followeeId.

Constraints:

  • 1≤1 \leq userId, followerId, followeeId ≤500\leq 500

  • 0≤0 \leq tweetId ≤104\leq 10^4

  • All the tweets have unique IDs.

  • At most 3×1043 \times 10^4 calls will be made to postTweet, getNewsFeed, follow, and unfollow.

  • A user cannot follow himself.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Design Twitter

Try to solve the Design Twitter problem.

Statement

Design a simplified version of a social media service like Twitter where users can post tweets, follow or unfollow other users, and view the 1010 most recent tweets in their news feed.

Implement the Twitter class:

  • Twitter() Initializes the Twitter object.

  • void postTweet(int userId, int tweetId) Composes a new tweet with ID tweetId published by the user userId. Each call to this function is made with a unique tweetId.

  • List[int] getNewsFeed(int userId) Retrieves the 1010 most recent tweet IDs in the user’s news feed. Each item in the news feed must be posted by users whom the user follows or by the user themselves. Tweets must be ordered from most recent to least recent.

  • void follow(int followerId, int followeeId) The user with ID followerId starts following the user with ID followeeId.

  • void unfollow(int followerId, int followeeId) The user with ID followerId unfollows the user with ID followeeId.

Constraints:

  • 1≤1 \leq userId, followerId, followeeId ≤500\leq 500

  • 0≤0 \leq tweetId ≤104\leq 10^4

  • All the tweets have unique IDs.

  • At most 3×1043 \times 10^4 calls will be made to postTweet, getNewsFeed, follow, and unfollow.

  • A user cannot follow himself.