Skip to content

Practice Design Twitter

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#355
LeetCode Problem

Design Twitter

Problem Statement

Twitter supports postTweet(userId,tweetId), follow(followerId,followeeId), unfollow(followerId,followeeId), and getNewsFeed(userId). A feed contains up to ten latest tweet IDs from the user and currently followed users, newest first. Tweet IDs are unique; posting order, not ID magnitude, defines recency. Practice approach: Store follow sets and per-user timestamped tweet lists. Merge the latest entries of eligible lists with a max-heap until ten tweets are collected. A user's own tweets must remain eligible independently of follow relationships. Test an empty feed, unfollowing, and posts made before following.

Source: Design Twitter on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["Twitter","postTweet","postTweet","follow","getNewsFeed","unfollow","getNewsFeed"]
[[],[1,20],[2,10],[1,2],[1],[1,2],[1]]
Output
[null,null,null,null,[10,20],null,[20]]
Explanation

Tweet 10 is newer despite its smaller ID. Unfollowing user 2 removes it from user 1's feed.

Constraints

1 <= user IDs <= 500
0 <= tweetId <= 10^4
Tweet IDs are unique.
At most 3 * 10^4 total method calls.
Users cannot follow themselves.

Related LeetCode Problems

Practice more problems similar to Design Twitter.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.