Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the 10 most recent tweets in the user's news feed.
Implement the Twitter class:
Twitter() Initializes your twitter object.void postTweet(int userId, int tweetId) Composes a new tweet with ID tweetId by the user userId. Each call to this function will be made with a unique tweetId.List<Integer> getNewsFeed(int userId) Retrieves the 10 most recent tweet IDs in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user themself. Tweets must be ordered from most recent to least recent.void follow(int followerId, int followeeId) The user with ID followerId started following the user with ID followeeId.void unfollow(int followerId, int followeeId) The user with ID followerId started unfollowing the user with ID followeeId.Example 1:
Input ["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"] [[], [1, 5], [1], [1, 2], [2, 6], [1], [1, 2], [1]] Output [null, null, [5], null, null, [6, 5], null, [5]] Explanation Twitter twitter = new Twitter(); twitter.postTweet(1, 5); // User 1 posts a new tweet (id = 5). twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5]. return [5] twitter.follow(1, 2); // User 1 follows user 2. twitter.postTweet(2, 6); // User 2 posts a new tweet (id = 6). twitter.getNewsFeed(1); // User 1's news feed should return a list with 2 tweet ids -> [6, 5]. Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5. twitter.unfollow(1, 2); // User 1 unfollows user 2. twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5], since user 1 is no longer following user 2.
Constraints:
1 <= userId, followerId, followeeId <= 5000 <= tweetId <= 1043 * 104 calls will be made to postTweet, getNewsFeed, follow, and unfollow.The key to solving #355 Design Twitter is designing a system that efficiently stores tweets and retrieves the 10 most recent tweets from a user and the people they follow. A common approach uses a hash map to map each user to the set of users they follow and another structure to store their tweets with timestamps.
Each tweet can be stored in a linked list or appended to a per-user list while maintaining a global increasing timestamp. When generating the news feed, we gather the latest tweets from the user and their followees and merge them using a min/max heap (priority queue) to extract the most recent ones efficiently.
This design ensures that operations like postTweet, follow, and unfollow remain fast, while getNewsFeed efficiently retrieves only the top 10 tweets rather than scanning all tweets. The approach balances quick updates with efficient retrieval, which is essential in real-world social feed systems.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| postTweet | O(1) | O(T) |
| follow / unfollow | O(1) | O(U + F) |
| getNewsFeed | O(F log F) | O(F) |
NeetCode
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
A heap allows efficient retrieval of the most recent tweets when combining tweets from multiple users. Instead of sorting all tweets, the heap ensures we only extract the top 10 most recent ones.
Yes, Design Twitter is a popular system design–style data structure question frequently asked in coding interviews at companies like Amazon, Meta, and Google because it tests design thinking and data structure knowledge.
Hash maps, linked lists (or lists), and heaps are commonly used. Hash maps track users and follow relationships, while a heap helps merge recent tweets from multiple users in chronological order.
The optimal approach uses hash maps to store follow relationships and tweet lists, combined with timestamps. A priority queue (heap) helps retrieve the 10 most recent tweets efficiently from the user and their followees.