250. Design Twitter

Create a simplified version of a social media platform similar to Twitter. Users should be able to post tweets, follow or unfollow other users, and view the 10 most recent tweets in their news feed.

Implement the Twitter class as follows:

  • Twitter() initializes the twitter object
  • void postTweet(int userId, int tweetId) It composes a new tweet with ID tweetId by the user userId. All tweetId's will be unique.
  • List<Integer> getNewsFeed(int userId) Retrieves the 10 most recent tweet IDs in the user's news feed. The news feed should only show posts from users the user follows or from the user themself, with tweets arranged from most recent to least recent.
  • void follow(int followerId, int followeeId) The user with ID followerId started following the user with ID followeeId. Input will be given such that user with followerId will not be following user with followeeID at the time of function call.
  • void unfollow(int followerId, int followeeId) The user with ID followerId unfollowed the user with ID followeeId. Input will be given such that user with followerId will be following user with followeeID at the time of function call.

Example 1:

Input: [postTweet(1, 2), postTweet(2, 6), getNewsFeed(1), follow(1, 2), getNewsFeed(1), unfollow(1, 2), postTweet(1,7), getNewsFeed(1)]

Output:[null, null, [2], null, [6, 2], null, [7, 2]]

Explanation:

postTweet(1, 2): User with userId 1 posts tweet with tweetId 2.

postTweet(2, 6): User with userId 2 posts tweet with tweetId 6.

getNewsFeed(1): Outputs feed of userId 1 = [2].

follow(1, 2): User with userId 1 follows user with userId 2.

getNewsFeed(1): Outputs feed of userId 1 = [6, 2], it contains the news of userId 2 as well.

unfollow(1, 2): User with userId 1 unfollows user with userId 2.

postTweet(1,7): User with userId 1 posts tweet with tweetId 7.

getNewsFeed(1): Outputs feed of userId 1 = [7, 2].

Example 2:

Input: [follow(2, 1), follow(1, 2), postTweet(1, 6), getNewsFeed(1), getNewsFeed(2), unfollow(2, 1), getNewsFeed(2)]

Output: [null, null, null, [6], [6], null, []]

Explanation:

follow(2, 1): User with userId 2 follows user with userId 1.

follow(1, 2): User with userId 1 follows user with userId 2.

postTweet(1, 6): User with userId 1 posts tweet with tweetId 6.

getNewsFeed(1): Outputs feed of userId 1 = [6].

getNewsFeed(2): Outputs feed of userId 2 = [6]. This includes tweets of userId 1 as well.

unfollow(2, 1): User with userId 2 unfollows user with userId 1.

getNewsFeed(2): Outputs feed of userId 2 = [], its empty as userId 2 no longer follows userId 1.

Now Your Turn!

Pick the correct output for the given input

Input: [postTweet(1, 1), postTweet(2, 2), follow(1, 2), unfollow(1, 2), follow(1, 2), getNewsFeed(1)]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= Number of instructions <= 104
  • 0 <= tweetId<= 104
  • 1 <= Number of UserId, followerId, followeeId <= 500
  • tweetId will all be unique
  • followerId != followeeId on the same call
  • All input will be valid

Fun Facts

0
class Twitter {
public:
Twitter() {
 
}
 
void postTweet(int userId, int tweetId) {
 
}
 
vector<int> getNewsFeed(int userId) {
 
}
 
void follow(int followerId, int followeeId) {
 
}
void unfollow(int followerId, int followeeId) {
 
}
};
Test Case

Input:

Nums