Design a small version of Twitter. Users post tweets and follow each other, and each user has a news feed: the 10 most recent tweets posted by the user and by the people the user follows, newest first.
Implement the class Twitter:
Twitter()creates an empty Twitter: no users, no tweets.postTweet(userId, tweetId)posts a new tweet with the idtweetIdby the useruserId. Each call posts later than all the calls before it.getNewsFeed(userId)returns the ids of the 10 most recent tweets in the user's news feed, newest first. A feed with fewer than 10 tweets returns all of them, and an empty feed returns no ids at all.follow(followerId, followeeId): the userfollowerIdstarts following the userfolloweeId.unfollow(followerId, followeeId): the userfollowerIdstops following the userfolloweeId.
Following is one way: when user 1 follows user 2, user 1 sees user 2's tweets, old and new, but not the other way round. Following someone you already follow, following yourself and unfollowing someone you don't follow change nothing; your own tweets are always in your feed.
The input is a list of operations, applied in order to one new Twitter. The output has one line for each getNewsFeed call: the tweet ids it returns, separated by spaces, or an empty line for an empty feed. The starter code spells these names the way your language does.
Example 1:
Input:
postTweet(1, 5)
getNewsFeed(1)
follow(1, 2)
postTweet(2, 6)
getNewsFeed(1)
unfollow(1, 2)
getNewsFeed(1)
Output:
5
6 5
5
Explanation:
User 1's first feed holds their own tweet 5.
User 1 then follows user 2, who posts tweet 6: it is newer than 5, so it comes first.
After the unfollow, user 2's tweets leave user 1's feed again.
Example 2:
Input:
follow(1, 2)
postTweet(1, 101)
postTweet(2, 201)
postTweet(1, 102)
postTweet(2, 202)
...
postTweet(1, 106)
postTweet(2, 206)
getNewsFeed(1)
getNewsFeed(2)
Output:
206 106 205 105 204 104 203 103 202 102
206 205 204 203 202 201
Explanation:
Users 1 and 2 take turns posting 6 tweets each (101 to 106 and 201 to 206).
User 1's feed has all 12, but shows only the 10 newest: 101 and 201 are left out.
User 2 doesn't follow user 1, so user 2's feed holds only user 2's own tweets.
Example 3:
Input:
getNewsFeed(3)
follow(3, 3)
postTweet(3, 7)
getNewsFeed(3)
unfollow(3, 4)
follow(3, 4)
getNewsFeed(3)
postTweet(4, 8)
getNewsFeed(3)
Output:
7
7
8 7
Explanation:
The first line of the output is empty: nobody has posted yet, so user 3's feed is empty.
follow(3, 3) changes nothing: tweet 7 appears once. unfollow(3, 4) changes nothing either: user 3 didn't follow user 4.
User 3 follows user 4 before user 4 posts anything; once tweet 8 arrives, it is in user 3's feed.
Constraints:
1 <= userId, followerId, followeeId <= 5000 <= tweetId <= 109- All the tweets have different ids.
- At most
105calls in total are made topostTweet,getNewsFeed,followandunfollow.
Note:
postTweet, follow and unfollow should run in O(1) time, and getNewsFeed in O(F log F) time, where F is the number of people the user follows. Collecting and sorting all the tweets of those people at every getNewsFeed would take time that grows with every tweet ever posted.
Free plan: 30 tutor messages. PRO is unlimited.
