Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Design Twitter

Easy
0/40
profile
Contributed by
9 upvotes

Problem statement

You must design a simplified version of Twitter, where a user can post tweets, follow/unfollow other users and can view the 10 latest tweets in the news feed.


You must implement the following methods:

• postTweet (userId, tweetId): Creates a new tweet.

• getNewsFeed(userId): Returns the array of 10 latest tweet ids (return all if there are fewer than 10 tweets overall). Users, who the user follows or the user themselves, must have posted each item in the news feed. The most recent tweets must come first, followed by the least recent.

• follow (followerId, followeeId): Follower follows the followee.

• unfollow(followerId, followeeId): Follower unfollows a followee.
Example:
Input: 

postTweet(0, 101), postTweet(1, 102), getNewsFeed(0), follow(1, 0), getNewsFeed(1)

Output:

101
102 101

Explanation: 

• postTweet(0, 101): A user with userId 0 will post a new tweet with id 101.
• postTweet(1, 102): A user with userId 1 will post a new tweet with id 102.
• getNewsFeed(0): Returns a list with only one element 101.
• follow(1, 0): User with userId 1 starts following the user with userId 0.
• getNewsFeed(1): Returns a list with elements 102 and 101, respectively.
Operations Performed:
Query-1 (Denoted by an integer 1)- postTweet (userId, tweetId)
Query-2 (Denoted by an integer 2)- getNewsFeed(userId)
Query-3 (Denoted by an integer 3)- follow (followerId, followeeId)
Query-4 (Denoted by an integer 4)- unfollow(followerId, followeeId)
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first input line contains an integer ‘Q’ denoting the number of queries.

The next ‘Q’ lines represent the queries that must be performed.

Each query consists of multiple integers that are separated by a single space. The first integer in each query represents the type of query to be performed, while the subsequent integers serve as parameters or values needed for that specific query.
Output format
The output consists of 'X' lines, where 'X' represents the count of Query-2 operations performed. 

Each line in the output consists of several integers separated by spaces, representing the result of the 'getNewsFeed' query.
Note:
You do not need to print anything; it has already been handled. Just implement the given function.
Sample Input 1:
7
1 0 101
1 1 102
1 2 103
2 0
3 0 1
3 0 2
2 0 
Sample Output 1:
101
103 102 101
Explanation Of Sample Input 1:
• postTweet(0, 101): A user with userId 0 will post a new tweet with id 101.
• postTweet(1, 102): A User with userId 1 will post a new tweet with id 102.
• postTweet(2, 103): A User with userId 1 will post a new tweet with id 103.
• getNewsFeed(0): Returns a list with only one element 101.
• follow(0, 1): The user with userId 0 starts following the user with userId 1.
• follow(0, 2): The user with userId 0 starts following the user with userId 2.
 • getNewsFeed(0): Returns a list with elements 103, 102, and 101, respectively.
Sample Input 2:
2
1 0 101
2 0


Sample Output 2:
101
Constraints:
1 <= q <= 10^3
1 <= query type <= 4
0 <= userId, tweetId, followerId, followeeId <= 10^6
Time Limit: 1 sec
Full screen
Console