gitGood.dev
← Back to problems

Design Twitter

HardSystem Design

Description

Design a simplified Twitter where users can post tweets, follow / unfollow other users, and read the **10 most recent tweets** in their news feed. Operations: - `postTweet(userId, tweetId)`: composes a new tweet with id `tweetId` by user `userId`. Each call uses a unique `tweetId`. - `getNewsFeed(userId)`: retrieve the 10 most recent tweet ids in the user's news feed. Each item must be posted by users the user follows or by the user themself. Tweets must be ordered from most recent to least recent. - `follow(followerId, followeeId)`: `followerId` follows `followeeId`. - `unfollow(followerId, followeeId)`: `followerId` unfollows `followeeId`. (No-op if not currently following or if unfollowing self.) **For the auto-grader:** implement `twitterOps(operations)`. Each op is: - `["postTweet", userId, tweetId]` → `null` - `["getNewsFeed", userId]` → number[] (ids, most recent first, up to 10) - `["follow", followerId, followeeId]` → `null` - `["unfollow", followerId, followeeId]` → `null`
Example 1
Input: operations = [["postTweet",1,5],["getNewsFeed",1],["follow",1,2],["postTweet",2,6],["getNewsFeed",1],["unfollow",1,2],["getNewsFeed",1]]
Output: [null,[5],null,null,[6,5],null,[5]]
Explanation: After user 1 follows user 2 and user 2 posts tweet 6, user 1's feed is [6, 5]. After unfollow, user 1 only sees their own tweet [5].

Constraints

- 1 <= userId, followerId, followeeId <= 500 - 0 <= tweetId <= 10^4 - All tweetIds within a single test are unique. - 1 <= operations.length <= 3 * 10^4 - Tweets are inserted in the order operations appear; treat each postTweet as one logical timestamp tick.
Loading editor...