Implement a simplified version of Twitter which allows users to post tweets, follow/unfollow each other, and view the 10 most recent tweets within their own news feed.
Users and tweets are uniquely identified by their IDs (integers).
Implement the following methods:
Twitter() Initializes the twitter object.void postTweet(int userId, int tweetId) Publish a new tweet with ID tweetId by the user userId. You may assume that each tweetId is unique.List<Integer> getNewsFeed(int userId) Fetches at most the 10 most recent tweet IDs in the user's news feed. Each item must be posted by users who the user is following or by the user themself. Tweets IDs should be ordered from most recent to least recent.void follow(int followerId, int followeeId) The user with ID followerId follows the user with ID followeeId.void unfollow(int followerId, int followeeId) The user with ID followerId unfollows the user with ID followeeId.Example 1:
Input:
["Twitter", "postTweet", [1, 10], "postTweet", [2, 20], "getNewsFeed", [1], "getNewsFeed", [2], "follow", [1, 2], "getNewsFeed", [1], "getNewsFeed", [2], "unfollow", [1, 2], "getNewsFeed", [1]]
Output:
[null, null, null, [10], [20], null, [20, 10], [20], null, [10]]
Explanation:
Twitter twitter = new Twitter();
twitter.postTweet(1, 10); // User 1 posts a new tweet with id = 10.
twitter.postTweet(2, 20); // User 2 posts a new tweet with id = 20.
twitter.getNewsFeed(1); // User 1's news feed should only contain their own tweets -> [10].
twitter.getNewsFeed(2); // User 2's news feed should only contain their own tweets -> [20].
twitter.follow(1, 2); // User 1 follows user 2.
twitter.getNewsFeed(1); // User 1's news feed should contain both tweets from user 1 and user 2 -> [20, 10].
twitter.getNewsFeed(2); // User 2's news feed should still only contain their own tweets -> [20].
twitter.unfollow(1, 2); // User 1 unfollows user 2.
twitter.getNewsFeed(1); // User 1's news feed should only contain their own tweets -> [10].Constraints:
1 <= userId, followerId, followeeId <= 1000 <= tweetId <= 1000
You should aim for a solution with O(nlogn) time for each getNewsFeed() function call, O(1) time for the remaining methods, and O((N * m) + (N * M) + n) space, where n is the number of followeeIds associated with the userId, m is the maximum number of tweets by any user, N is the total number of userIds, and M is the maximum number of followees for any user.
Can you think of a data structure to store all the information, such as userIds and corresponding followeeIds, or userIds and their tweets? Maybe you should think of a hash data structure in terms of key-value pairs. Also, can you think of a way to determine that a tweet was posted before another tweet?
We use a hash map followMap to store userIds and their unique followeeIds as a hash set. Another hash map, tweetMap, stores userIds and their tweets as a list of (count, tweetId) pairs. A counter count, incremented with each tweet, tracks the order of tweets. The variable count is helpful in distinguishing the time of tweets from two users. This way of storing data makes the functions follow(), unfollow(), and postTweet() run in O(1). Can you think of a way to implement getNewsFeed()? Maybe consider a brute force approach and try to optimize it.
A naive solution would involve taking the tweets of the userId and its followeeIds into a list, sorting them in descending order based on their count values, and returning the top 10 tweets as the most recent ones. Can you think of a more efficient approach that avoids collecting all tweets and sorting? Perhaps consider a data structure and leverage the fact that each user's individual tweets list is already sorted.
We can use a Max-Heap to efficiently retrieve the top 10 most recent tweets. For each followee and the userId, we insert their most recent tweet from the tweetMap into the heap, along with the tweet's count and its index in the tweet list. This index is necessary because after processing a tweet, we can insert the next most recent tweet from the same user's list. By always pushing and popping tweets from the heap, we ensure that the 10 most recent tweets are retrieved without sorting all tweets.
Before attempting this problem, you should be comfortable with:
Each user has their own tweets, and they also see tweets from all the people they follow.
To build the news feed:
10 tweet IDs.This works because sorting guarantees we always pick the latest 10 tweets, regardless of who posted them.
Maintain:
tweetMap[user] -> list of (time, tweetId)followMap[user] -> set of users they followtime -> increases every time someone posts a tweetpostTweet(user, tweetId)
(current_time, tweetId) in that user's listtimegetNewsFeed(user)
10 tweet IDsfollow(follower, followee)
unfollow(follower, followee)
class Twitter:
def __init__(self):
self.time = 0
self.followMap = defaultdict(set)
self.tweetMap = defaultdict(list)
def postTweet(self, userId: int, tweetId: int) -> None:
self.tweetMap[userId].append((self.time, tweetId))
self.time += 1
def getNewsFeed(self, userId: int) -> List[int]:
feed = self.tweetMap[userId][:]
for followeeId in self.followMap[userId]:
feed.extend(self.tweetMap[followeeId])
feed.sort(key=lambda x: -x[0])
return [tweetId for _, tweetId in feed[:10]]
def follow(self, followerId: int, followeeId: int) -> None:
if followerId != followeeId:
self.followMap[followerId].add(followeeId)
def unfollow(self, followerId: int, followeeId: int) -> None:
self.followMap[followerId].discard(followeeId)Where is the total number of associated with the , is the maximum number of tweets by any user, is the total number of tweets associated with the and its , is the total number of and is the maximum number of followees for any user.
Each user can follow many people, and each of those people may have many tweets.
Instead of combining all tweets and sorting them (which is slow), we only need the 10 most recent tweets.
We use a min-heap (priority queue) because:
count), where smaller means more recent.This ensures:
10 tweets we need.Maintain:
tweetMap[user] -> list of (time, tweetId), sorted by recencyfollowMap[user] -> set of followeescount -> a decreasing timestamp for ordering tweetspostTweet(user, tweetId)
(count, tweetId) in the user's listcount (more negative = more recent)getNewsFeed(user)
[time, tweetId, followeeId, nextIndex]10 tweets:follow(follower, followee)
unfollow(follower, followee)
class Twitter:
def __init__(self):
self.count = 0
self.tweetMap = defaultdict(list) # userId -> list of [count, tweetIds]
self.followMap = defaultdict(set) # userId -> set of followeeId
def postTweet(self, userId: int, tweetId: int) -> None:
self.tweetMap[userId].append([self.count, tweetId])
self.count -= 1
def getNewsFeed(self, userId: int) -> List[int]:
res = []
minHeap = []
self.followMap[userId].add(userId)
for followeeId in self.followMap[userId]:
if followeeId in self.tweetMap:
index = len(self.tweetMap[followeeId]) - 1
count, tweetId = self.tweetMap[followeeId][index]
heapq.heappush(minHeap, [count, tweetId, followeeId, index - 1])
while minHeap and len(res) < 10:
count, tweetId, followeeId, index = heapq.heappop(minHeap)
res.append(tweetId)
if index >= 0:
count, tweetId = self.tweetMap[followeeId][index]
heapq.heappush(minHeap, [count, tweetId, followeeId, index - 1])
return res
def follow(self, followerId: int, followeeId: int) -> None:
self.followMap[followerId].add(followeeId)
def unfollow(self, followerId: int, followeeId: int) -> None:
if followeeId in self.followMap[followerId]:
self.followMap[followerId].remove(followeeId)Where is the total number of associated with the , is the maximum number of tweets by any user, is the total number of and is the maximum number of followees for any user.
The basic heap solution looks at all tweets of all followees, which is fast enough but can be improved.
Key observation:
10 tweets, because the news feed returns at most 10 items.10 most recent tweets.The trick:
count) and keep only the last 10 tweets.10), we first gather only the recent tweets that could appear in the final 10, using a max-heap limited to size 10.10 tweets per followee, and never extract more than 10 results.This makes the method very fast even when users post a lot of tweets.
postTweet(user, tweetId)
(timestamp, tweetId) at the end of that user's list.10, remove the oldest tweet.getNewsFeed(user)
Steps:
10:10 most recent tweets across followees.10 tweetsfollow(follower, followee)
unfollow(follower, followee)
class Twitter:
def __init__(self):
self.count = 0
self.tweetMap = defaultdict(list) # userId -> list of [count, tweetIds]
self.followMap = defaultdict(set) # userId -> set of followeeId
def postTweet(self, userId: int, tweetId: int) -> None:
self.tweetMap[userId].append([self.count, tweetId])
if len(self.tweetMap[userId]) > 10:
self.tweetMap[userId].pop(0)
self.count -= 1
def getNewsFeed(self, userId: int) -> List[int]:
res = []
minHeap = []
self.followMap[userId].add(userId)
if len(self.followMap[userId]) >= 10:
maxHeap = []
for followeeId in self.followMap[userId]:
if followeeId in self.tweetMap:
index = len(self.tweetMap[followeeId]) - 1
count, tweetId = self.tweetMap[followeeId][index]
heapq.heappush(maxHeap, [-count, tweetId, followeeId, index - 1])
if len(maxHeap) > 10:
heapq.heappop(maxHeap)
while maxHeap:
count, tweetId, followeeId, index = heapq.heappop(maxHeap)
heapq.heappush(minHeap, [-count, tweetId, followeeId, index])
else:
for followeeId in self.followMap[userId]:
if followeeId in self.tweetMap:
index = len(self.tweetMap[followeeId]) - 1
count, tweetId = self.tweetMap[followeeId][index]
heapq.heappush(minHeap, [count, tweetId, followeeId, index - 1])
while minHeap and len(res) < 10:
count, tweetId, followeeId, index = heapq.heappop(minHeap)
res.append(tweetId)
if index >= 0:
count, tweetId = self.tweetMap[followeeId][index]
heapq.heappush(minHeap, [count, tweetId, followeeId, index - 1])
return res
def follow(self, followerId: int, followeeId: int) -> None:
self.followMap[followerId].add(followeeId)
def unfollow(self, followerId: int, followeeId: int) -> None:
if followeeId in self.followMap[followerId]:
self.followMap[followerId].remove(followeeId)Where is the total number of associated with the , is the maximum number of tweets by any user ( can be at most ), is the total number of and is the maximum number of followees for any user.
When building the news feed, a user should see their own tweets in addition to tweets from people they follow. A common mistake is only iterating through followees without including the user themselves.
# Wrong - missing user's own tweets
for followeeId in self.followMap[userId]:
# Only gets followees' tweets
# Correct - include user in the feed
self.followMap[userId].add(userId) # Add self before iterating
for followeeId in self.followMap[userId]:
# Now includes user's own tweetsThe heap-based solution requires careful attention to whether you need a min-heap or max-heap. Since we want the most recent tweets (highest timestamp), using a min-heap requires negating timestamps, while a max-heap extracts them directly.
# With min-heap, negate count to get max behavior
heapq.heappush(minHeap, [count, tweetId, ...]) # count is negative
# Common mistake: forgetting to negate when using min-heap
heapq.heappush(minHeap, [self.time, tweetId, ...]) # Wrong if time is positiveWhile the news feed logic may internally add the user to their own follow set, the follow() method should prevent explicit self-following to avoid duplicate entries or unexpected behavior.
# Wrong - allows self-follow
def follow(self, followerId: int, followeeId: int) -> None:
self.followMap[followerId].add(followeeId)
# Correct - prevent self-follow
def follow(self, followerId: int, followeeId: int) -> None:
if followerId != followeeId:
self.followMap[followerId].add(followeeId)