Design Twitter

mediumHeap / Priority Queueheap-merge of followees' recent tweetsSolve on LeetCode#355

Part of Practice: Scheduling & Streams

Build a tiny version of Twitter supporting four operations: postTweet(userId, tweetId) publishes a tweet; follow(followerId, followeeId) and unfollow(followerId, followeeId) manage who a user follows; and getNewsFeed(userId) returns the ids of the 10 most recent tweets, newest first, drawn from the user's own tweets and those of everyone they follow.

Users and tweets are plain integers, a user cannot follow themselves (silently ignore it), and up to 3 × 10^4 operations may be issued, so the feed query needs to be efficient rather than scanning every tweet ever posted.

Examples

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

User 1's first feed contains only their own tweet 5. After following user 2, who then posts tweet 6, the feed shows [6, 5] — newest first. After unfollowing, tweet 6 disappears from the feed.

Input:
follow(1, 2); postTweet(2, 3); postTweet(1, 1); getNewsFeed(1)
Output:
[1, 3]

The feed merges user 1's own tweet with followee 2's tweet, ordered by recency: tweet 1 was posted after tweet 3.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.