AlgoLounge

Design Twitter

medium

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and retrieve the 10 most recent tweet IDs in the user's news feed.

Implement the Twitter class:

  • Twitter() Initializes the twitter object.
  • postTweet(userId: int, tweetId: int) -> None Composes a new tweet with ID tweetId by the user userId. Each call to this function will have a unique tweetId.
  • getNewsFeed(userId: int) -> list[int] Retrieves the 10 most recent tweet IDs in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user themself. Tweets must be ordered from most recent to least recent.
  • follow(followerId: int, followeeId: int) -> None The user with ID followerId started following the user with ID followeeId.
  • unfollow(followerId: int, followeeId: int) -> None The user with ID followerId started unfollowing the user with ID followeeId.

Examples

Example 1

Input:  

["Twitter", "postTweet", "getNewsFeed", "follow", "getNewsFeed", "unfollow", "getNewsFeed"] [[], [1, 5], [1], [1, 2], [1], [1, 2], [1]] Output: [null, null, [5], null, [6, 5], null, [5]] twitter = Twitter() twitter.postTweet(1, 5) # User 1 posts tweet 5 twitter.getNewsFeed(1) # return [5], user 1's own tweet twitter.follow(1, 2) # User 1 follows user 2 twitter.postTweet(2, 6) # User 2 posts tweet 6 twitter.getNewsFeed(1) # return [6, 5], tweet 6 is more recent twitter.unfollow(1, 2) # User 1 unfollows user 2 twitter.getNewsFeed(1) # return [5], only user 1's own tweet

Constraints

  • 1 <= userId, followerId, followeeId <= 500
  • 0 <= tweetId <= 10⁴
  • All the tweets have unique IDs.
  • At most 3 * 10⁴ calls will be made to postTweet, getNewsFeed, follow, and unfollow.

How the tests work: each test is a sequence of calls. operations[i] is the method called with arguments[i]; the first entry constructs the class. The expected output lists what every call returns — null for the constructor and for methods that return nothing.

TypeScript
Loading editor…
List of test cases (10)
Case 1
operations = ["Twitter","postTweet","getNewsFeed","follow","postTweet","getNewsFeed","unfollow","getNewsFeed"]
arguments = [[],[1,5],[1],[1,2],[2,6],[1],[1,2],[1]]
Case 2
operations = ["Twitter","postTweet","postTweet","postTweet","getNewsFeed"]
arguments = [[],[1,1],[1,2],[1,3],[1]]
Case 3
operations = ["Twitter","getNewsFeed"]
arguments = [[],[1]]
Case 4
operations = ["Twitter","postTweet","follow","follow","postTweet","postTweet","getNewsFeed"]
arguments = [[],[1,10],[1,2],[1,3],[2,20],[3,30],[1]]
Case 5
operations = ["Twitter","follow","postTweet","getNewsFeed","unfollow","getNewsFeed"]
arguments = [[],[1,2],[2,99],[1],[1,2],[1]]
Case 6
operations = ["Twitter","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","getNewsFeed"]
arguments = [[],[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10],[1,11],[1]]
Case 7
operations = ["Twitter","postTweet","postTweet","follow","getNewsFeed","unfollow","follow","getNewsFeed"]
arguments = [[],[1,1],[2,2],[1,2],[1],[1,2],[1,2],[1]]
Case 8
operations = ["Twitter","follow","getNewsFeed"]
arguments = [[],[1,2],[1]]
Case 9
operations = ["Twitter","postTweet","follow","postTweet","follow","postTweet","getNewsFeed"]
arguments = [[],[1,100],[2,1],[2,200],[3,2],[3,300],[3]]
Case 10
operations = ["Twitter","postTweet","postTweet","follow","getNewsFeed","postTweet","getNewsFeed"]
arguments = [[],[2,50],[1,60],[1,2],[1],[2,70],[1]]