Design Twitter
Heap / Priority Queue
Asked at 12 companies
3 recent
- Palo Alto Networks
- Amazon
- X
- Meesho
- Swiggy
- Coupang
- Adobe
- Salesforce
- Apple
+2 moreShow fewer
- Microsoft
- Bloomberg
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Merge sort: split, sort, merge · Partitioning around a pivot
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Task schedulers and leaderboards: A heap always knows the most urgent job or the top score, so schedulers and “top 10” lists never re-sort everything.
About this problem
Keep the smallest or largest items on top: top-k, streams, and scheduling.
Read the full statement and examples on LeetCode, try it for 20–30 minutes, then come back for the walkthrough and the step-by-step visual.
Examples
Full statement on LeetCode ↗Example 1
- Input
- postTweet(1,5), follow(1,2), postTweet(2,6), getNewsFeed(1), unfollow(1,2), getNewsFeed(1)
- Output
- [6, 5], [5]
Walk through it step by step(7 steps · shows the approach, so try first)
- Each user keeps their own tweets with a timestamp, plus the set of people they follow.
- User 1 posts tweet 5 at time 1.
- User 1 follows user 2.
- User 2 posts tweet 6 at time 2.
- getNewsFeed(1): gather tweets from everyone user 1 follows (including themselves) and take the newest first — a by time does this efficiently. Feed: 6, 5.
- User 1 unfollows user 2.
- getNewsFeed(1): gather tweets from everyone user 1 follows (including themselves) and take the newest first — a max-heap by time does this efficiently. Feed: 5.
Code it on LeetCode
This one needs setup the editor here can't do yet (a class you build, linked nodes, or a helper LeetCode provides), so solve it on LeetCode, with its own tests. Then tick My code passed below, and it joins your reviews. With your LeetCode account linked, that happens on its own.
An error or a Wrong Answer there? What each one means.
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionStore each user's tweets with a global increasing timestamp. The feed is a k-way merge: put the newest tweet of the user and of everyone they follow in a , and pop up to 10 times, pushing each user's next-older tweet.
class Twitter:
def __init__(self):
self.time = 0
self.tweets = defaultdict(list) # user -> [(time, tweetId)]
self.following = defaultdict(set) # user -> followees
def postTweet(self, userId: int, tweetId: int) -> None:
self.time += 1
self.tweets[userId].append((self.time, tweetId))
def getNewsFeed(self, userId: int) -> List[int]:
heap = []
for u in self.following[userId] | {userId}:
if self.tweets[u]:
i = len(self.tweets[u]) - 1
t, tid = self.tweets[u][i]
heappush(heap, (-t, tid, u, i))
feed = []
while heap and len(feed) < 10:
_, tid, u, i = heappop(heap)
feed.append(tid)
if i > 0:
t, nxt = self.tweets[u][i - 1]
heappush(heap, (-t, nxt, u, i - 1))
return feed
def follow(self, followerId: int, followeeId: int) -> None:
if followerId != followeeId:
self.following[followerId].add(followeeId)
def unfollow(self, followerId: int, followeeId: int) -> None:
self.following[followerId].discard(followeeId)Reference solution, tested in Python, Java and C++.
Other ways to solve it
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.