Design a simplified version of Twitter where users can interact through posting tweets, following other users, and viewing personalized news feeds.

Your task is to implement a Twitter class that supports these core social media functionalities:

  • Post Tweets: Users can compose and publish tweets with unique IDs
  • Follow/Unfollow: Users can follow or unfollow other users to curate their network
  • News Feed: Users can retrieve their personalized feed showing the 10 most recent tweets from themselves and users they follow

The news feed should display tweets in chronological order (most recent first), making it feel like a real social media experience!

Class Methods to Implement:

  • Twitter() - Initialize the Twitter system
  • postTweet(userId, tweetId) - User posts a tweet
  • getNewsFeed(userId) - Get 10 most recent tweets for user's feed
  • follow(followerId, followeeId) - User starts following another user
  • unfollow(followerId, followeeId) - User unfollows another user

Input & Output

example_1.py — Basic Operations
$ Input: ["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed"] [[], [1, 5], [1], [1, 2], [2, 6], [1]]
Output: [null, null, [5], null, null, [6, 5]]
💡 Note: User 1 posts tweet 5, gets feed [5]. User 1 follows user 2. User 2 posts tweet 6. User 1's feed now shows [6, 5] - both their own tweet and their followee's tweet in chronological order.
example_2.py — Multiple Users and Tweets
$ Input: ["Twitter", "postTweet", "postTweet", "postTweet", "getNewsFeed", "follow", "getNewsFeed"] [[], [1, 1], [2, 2], [1, 3], [1], [1, 2], [1]]
Output: [null, null, null, null, [3, 1], null, [3, 2, 1]]
💡 Note: Multiple users post tweets. User 1's initial feed shows only their tweets [3, 1]. After following user 2, the feed includes user 2's tweets: [3, 2, 1].
example_3.py — Unfollow Operation
$ Input: ["Twitter", "postTweet", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"] [[], [1, 1], [1, 2], [2, 2], [1], [1, 2], [1]]
Output: [null, null, null, null, [2, 1], null, [1]]
💡 Note: User 1 follows user 2 and sees both tweets [2, 1]. After unfollowing user 2, the feed only shows user 1's own tweets [1].

Constraints

  • 1 ≤ userId, followerId, followeeId ≤ 500
  • 0 ≤ tweetId ≤ 104
  • All the tweets have unique IDs
  • At most 3 × 104 calls will be made to the functions

Visualization

Tap to expand
Design Twitter - Hash Map Approach INPUT Operations: 1. Twitter() 2. postTweet(1, 5) 3. getNewsFeed(1) 4. follow(1, 2) 5. postTweet(2, 6) 6. getNewsFeed(1) Data Structures: users Map userId --> Set(following) List(tweets) tweets Map userId --> [(tweetId, timestamp)] 1 User 1 2 User 2 follows ALGORITHM STEPS 1 Initialize Twitter Create empty HashMaps for users and tweets 2 postTweet(userId, tweetId) Store tweet with timestamp in user's tweet list 3 follow(followerId, followeeId) Add followeeId to follower's following Set 4 getNewsFeed(userId) Merge tweets from user + followed users, sort by time Max Heap (by timestamp): t=2 t=1 ... FINAL RESULT Execution Trace: Twitter() --> null postTweet(1,5) --> null getNewsFeed(1) --> [5] follow(1,2) --> null postTweet(2,6) --> null getNewsFeed(1) --> [6,5] Output Array: [null,null,[5],null,null,[6,5]] Final State: User 1 tweets:[5] follows:{2} User 2 tweets:[6] follows:{} Key Insight: Use HashMap for O(1) user lookup. Store tweets with timestamps for chronological ordering. For getNewsFeed: collect tweets from user + followed users, use Max Heap to get top 10 most recent. Time: postTweet O(1), follow/unfollow O(1), getNewsFeed O(N log K) where N=total tweets, K=10 TutorialsPoint - Design Twitter | Hash Map Approach
Asked in
Meta 45 Twitter 38 Google 32 Amazon 28
64.8K Views
High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen