DSA Tracker

Problem

Medium

Design Twitter

A medium Heap interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Heap
Sheets
1
Core for
10 roles

The problem

Design a simplified Twitter where users can post tweets, follow or unfollow other users, and see the 10 most recent tweets in their news feed. A user's news feed should include tweets from themselves and the users they follow, ordered from most recent to least recent.

Example 1

Input
postTweet(1, 5), getNewsFeed(1) -> [5], follow(1, 2), postTweet(2, 6), getNewsFeed(1) -> [6, 5], unfollow(1, 2), getNewsFeed(1) -> [5]
Output
[null, [5], null, null, [6, 5], null, [5]]
Why
User 1 posts tweet 5. Their feed shows [5]. User 1 follows user 2. User 2 posts tweet 6. Now user 1's feed includes both tweets [6, 5] (most recent first). After unfollowing user 2, the feed reverts to just [5].

Constraints

  • 1 <= user_id, follower_id, followee_id <= 500
  • 0 <= tweet_id <= 10^4
  • At most 3 * 10^4 calls will be made in total

How to think about it

Updated 2026-09-29

Every user produces a chronological list of their own posts, so generating a feed is nothing more than merging several sorted lists in reverse order. You only ever need at most 10 items, which means pulling the newest tweet across followed users via a k-way merge without scanning everyone's entire history.

Approaches, worst first

  1. Collect all and sort

    time O(u * t log(u * t)) · space O(u * t)

    Iterate over every followee plus the user, collect all their historical tweets into a single array, sort descending by global timestamp, and slice the first 10. Simple, but dumps and sorts thousands of old tweets that have zero chance of surfacing.

  2. Multi-way merge with max-heapWrite this one

    time O(u + 10 log u) · space O(u)

    Initialize a max-heap with the most recent tweet of each followee and the user. Pop the globally latest tweet, then push the next older tweet from that exact same user into the heap. Terminate after extracting 10 tweets.

Where people lose marks · 3
  • A user must always see their own tweets in their news feed, even if they do not explicitly follow themselves.
  • Allowing a user to follow or unfollow themselves. If self-following is recorded in the follow graph, unfollowing could remove self-tweets or cause duplicate feeds.
  • Sorting by tweetId instead of an incrementing logical timestamp. Tweet IDs are arbitrary and do not necessarily arrive in monotonically increasing chronological order.

The theory behind it

Heap — the ground this problem stands on. All Heap problems

What Heap is

A heap is a specialized tree that keeps only the single most extreme item at the very top. In a min-heap, every parent node is smaller than its children, so the smallest element in the entire collection sits immediately at the root. Unlike a binary search tree, a heap does not keep all items in full sorted order. It maintains only a partial order, making it fast at giving you the single smallest or largest item without spending time sorting everything else.

When to reach for it

Reach for a heap when a problem asks for the top k largest elements, the kth smallest value, or a running median from a stream of numbers. Signals include phrases like continuously finding the cheapest item, merging k sorted linked lists, or scheduling tasks with priorities. Whenever you need repeated access to the minimum or maximum value while items are added and removed dynamically, a priority heap is the tool.

How the pattern works

To find the k largest elements, keep a min-heap of fixed size k. Push incoming numbers into the heap; whenever the heap size grows past k, pop the top item, which is the smallest among them. After processing all elements, only the k largest remain. For a running median, balance two heaps: a max-heap holding the smaller half of numbers and a min-heap holding the larger half. In code, heaps are stored compactly as flat arrays where a node at index i has children at indices 2i plus 1 and 2i plus 2.

More Heap problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .