Build the engine of a small social app. People post short messages called tweets, follow each other, and read a feed of the latest tweets.
Write a class Twitter that supports these calls:
Twitter() starts an app with no users and no tweets.
void postTweet(int userId, int tweetId) saves the tweet tweetId, written by the user userId. No two calls ever use the same tweetId.
List<Integer> getNewsFeed(int userId) returns up to 10 tweet ids, newest first. They come from userId and from every user that userId follows.
void follow(int followerId, int followeeId) makes followerId follow followeeId.
void unfollow(int followerId, int followeeId) makes followerId stop following followeeId.
Examples
Example 1:
Input
["Twitter","postTweet","postTweet","getNewsFeed","follow","getNewsFeed","unfollow","getNewsFeed"]
[[],[3,101],[4,202],[3],[3,4],[3],[3,4],[3]]
Output
[null,null,null,[101],null,[202,101],null,[101]]
Explanation
The first line lists the calls and the second line lists their arguments.
User 3 posts tweet 101 and user 4 posts tweet 202.
User 3 follows nobody yet, so the feed of user 3 is [101].
After user 3 follows user 4, the feed is [202,101], with the newest tweet first.
After user 3 unfollows user 4, the feed goes back to [101].
Example 2:
Input
["Twitter","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","getNewsFeed"]
[[],[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10],[1,11],[1]]
Output
[null,null,null,null,null,null,null,null,null,null,null,null,[11,10,9,8,7,6,5,4,3,2]]
Explanation
User 1 posts 11 tweets. The feed keeps only the 10 newest, so tweet 1 is not shown.