Design Twitter - Problem
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 systempostTweet(userId, tweetId)- User posts a tweetgetNewsFeed(userId)- Get 10 most recent tweets for user's feedfollow(followerId, followeeId)- User starts following another userunfollow(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
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
โก Linearithmic
Space Complexity
O(n + m)
n = number of users, m = total tweets stored
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code