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

Visualization

Tap to expand
User Hash Mapuser1 โ†’ [tweets]user2 โ†’ [tweets]user3 โ†’ [tweets]Following Graphuser1 โ†’ {user2}user2 โ†’ {user3}user3 โ†’ {user1}Tweet Storage(timestamp, id)(15, tweet_A)(12, tweet_B)Max HeapTweet PriorityQueueNews Feed Algorithm1. Get user's tweets2. Get followees' tweets3. Heap merge & extract top 10Final Feed[tweet_15][tweet_12][tweet_09]O(1) lookupTime: O(k log m)k = followees, m = tweets/user
Understanding the Visualization
1
Data Storage
Hash maps store user tweets and following relationships for O(1) access
2
Tweet Posting
New tweets are appended with timestamps for chronological ordering
3
News Feed Generation
Heap efficiently merges and selects top 10 tweets from multiple sources
4
Follow Management
Set operations handle follow/unfollow with no duplicates
Key Takeaway
๐ŸŽฏ Key Insight: Combining hash maps for O(1) access with heaps for efficient top-k selection creates a scalable social media system that handles real-time feeds efficiently.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(k log m)

k = number of followees, m = tweets per user, log m for heap operations

n
2n
โšก Linearithmic
Space Complexity
O(n + m)

n = number of users, m = total tweets stored

n
2n
โšก Linearithmic Space

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