DEV Community

Cover image for Design a top-K / trending system: the system design interview walkthrough
Gowtham Potureddi
Gowtham Potureddi

Posted on

Design a top-K / trending system: the system design interview walkthrough

Design a top-K / trending system: the system design interview walkthrough

The request to design a top-K system — the ten most-viewed videos, the trending hashtags, the leaderboard's top scorers — reads like one ORDER BY count DESC LIMIT 10. For a thousand items it is. But the interview hands you a firehose: billions of view events a day across millions of distinct items, a top-ten that must stay fresh to the second, and a query that answers in tens of milliseconds. Keeping an exact counter for every item and re-sorting them all on each event is hundreds of gigabytes of state and a write amplification that falls over — so the real question is how little can you remember and still name the leaders? This is a classic system design interview prompt because the naive count-and-sort is obviously right and obviously doesn't scale, and the interesting answer trades a little precision for an enormous amount of memory and speed.

The round turns on three decisions. First, exact top-K — when the item set is bounded (a game leaderboard), a sorted set gives you ranked top-K cheaply. Second, approximate top-K at scale — when the stream is enormous and the items are many, you stop counting exactly and use a probabilistic sketch plus a small heap. Third, windows and aggregation — "trending" means a time window, and at scale you shard the stream, compute a local top-K per shard, and merge those into a global top-K. This walkthrough follows the path a strong candidate draws on the whiteboard — requirements, estimate, API and data model, high-level design, then the deep dives — and at each step it names the follow-up the interviewer uses to tell a memorised answer from an engineer who understands the tradeoff.

Title card for a system design interview walkthrough on designing a top-K and trending system, showing a firehose of view events flowing into a counter and a ranked top-K leaderboard list, on a dark citrus-and-lavender background.

How the top-K round is actually run

A top-K round runs about 60 minutes on a shared canvas — you draw the boxes and arrows while you talk, and the interviewer interrupts. It is a flexible, favourite problem because the same shape fits a dozen prompts (trending videos, top hashtags, a game leaderboard, a most-played songs chart), and interviewers love to shift the requirements mid-round — exact to approximate, all-time to last-hour — to see if you can adapt. The spine is always the same: count a stream of events and surface the K biggest, cheaply. Get the exact-vs-approximate call and the windowed aggregation clean and you have shown the whole system in one problem.

The interviewer rarely lets "keep a counter per item and sort them" stand. "There are billions of views a day across tens of millions of videos — you cannot keep an exact live count for every one of them and re-sort on each view. So how do you name the top ten without remembering everything?" That question — top-K without an exact count of every item — is the one a copied answer can never rehearse, and the one this round is really testing.

The rounds here are modelled on how the loop runs, not scraped from any candidate's recall. What matters for prep is the shape: a blank canvas, a clock, and a follow-up after every decision you make.

The five-step framework for any HLD round

Every high-level design answer fits the same spine. Say it out loud at the start so the interviewer knows you have a plan, then walk it:

  1. Clarify requirements — exact or approximate, all-time or windowed, and how big K and the item set are.
  2. Estimate — events per second, distinct items, and the memory an exact count would need, to justify every later choice.
  3. High-level design — the boxes and arrows: event stream, aggregators, top-K store, query.
  4. Deep dive — the two or three decisions that actually decide this system.
  5. Follow-ups — bottlenecks, failure modes, and what you would change at ten times the scale.

What the debrief measures

The score is not "right or wrong design." A good HLD debrief reads on four dimensions, and every move below maps to one: problem solving (did you reach for an approximate sketch when exact counting blew the memory budget), communication (did you say why "trending" needs a time window, and what precision you traded, in a sentence), technical execution (a sorted set for exact leaderboards, count-min sketch plus a min-heap for approximate top-K, sharded local top-K merged to global), and time management (did you spend your 60 minutes on counting and aggregation, not on the video-metadata schema). Reading a walkthrough teaches the design; it does not teach you to deliver it on the clock while someone pushes back.

Step 1 — Requirements: what a top-K system must do

Spend the first few minutes here — and the very first question decides the whole design: exact or approximate?

Type Requirement
Functional Ingest a stream of events (a view, a play, a score) keyed by item; return the top K items by count; support a time scope (all-time and/or "trending in the last window"); return a rank or score for an item (for leaderboards).
Functional (optional) Exact per-item counts on demand, percentile/rank queries, multiple leaderboards (per game/region), decay so old events fade — call these out, then scope them.
Non-functional Very high write throughput (a firehose of events); low-latency top-K reads (tens of ms); approximate is usually acceptable for trending (not for a money leaderboard); memory-efficient at high item cardinality; horizontally scalable; fault-tolerant.

The sentence that earns points: "The first fork is exactness — a bounded leaderboard can keep an exact sorted set, but a billion-event trending stream over tens of millions of items cannot count everything, so there I trade precision for a probabilistic sketch plus a heap, and 'trending' means a time window." Say that and you have framed the entire design before drawing a box.

Step 2 — Capacity estimation (back of the envelope)

Estimation here is about proving one thing: at scale, an exact count of every item does not fit, which is what forces the approximate approach.

Quantity Assumption Result
Events billions/day ~1M+ events/sec at peak → a streaming write path, not a per-event DB write
Distinct items tens of millions an exact hash-map counter would be hundreds of GB of hot state
Approximate count count-min sketch the same frequency estimates in hundreds of MB → fits in memory
K small (top 10–1000) a min-heap of size K is tiny; the cost is counting, not ranking
Read latency tens of ms serve top-K from a precomputed, cached structure, never a scan

Two conclusions fall out, and you should say both out loud. First, exact counting does not scale for a high-cardinality stream — keeping a live counter for tens of millions of items is hundreds of gigabytes and a write per event, so for trending you switch to a count-min sketch (hundreds of megabytes) and accept estimates. Second, ranking is cheap; counting is the cost — K is small, so a min-heap of the current leaders is trivial; all the work is absorbing the event firehose and keeping frequencies, which is why the design is a streaming aggregation, not a database query. You have now pointed the interview at exact-vs-approximate and the aggregation — the real deep dives — before anyone asked.

Step 3 — API and data model

The interface is a write (ingest events) and a read (top-K); the design lives in the counting structure behind them.

Call Operation
record(item_id, ts) append an event to the stream; aggregation updates the count/sketch
topK(k, window) → items[] read the current top K for a scope (all-time or a time window) from the precomputed structure
rank(item_id) → position / getScore(item_id) → count for a leaderboard: the exact rank or score of one item

The data model is the event and the two counting structures — an exact sorted set, and an approximate sketch plus heap:

Entity Notes
event item_id, ts (and a count/score for leaderboards). The raw stream.
sorted_set item → score, kept ranked (a Redis ZSET); exact top-K and rank for a bounded item set; O(log n) update, O(k) top-read.
count_min_sketch a probabilistic frequency table: estimates an item's count in sublinear memory by hashing, forgetting the items themselves. For high-cardinality streams.
min_heap[K] the current top-K leaders by estimated count — the sketch says how many, the heap tracks which are winning.

Step 4 — High-level design: the streaming top-K pipeline

Now the boxes. A top-K system is a streaming aggregation that turns an event firehose into a small ranked list:

  • Producers → event stream (Kafka): every view/play/score is emitted to a durable, partitioned log that absorbs the firehose and decouples producers from the counters.
  • Aggregation workers (per shard): each consumer owns a partition and maintains a local counting structure — a count-min sketch plus a min-heap of its local top-K (approximate), or exact counters for a bounded leaderboard.
  • Merge / global top-K: a merge step combines the shards' local top-Ks into the global top-K and writes it to a fast store (a sorted set in cache) — because the true global leaders are almost always local leaders in some shard, merging local top-Ks recovers the global answer cheaply.
  • Top-K store + query service: the current top-K (and the leaderboard ZSET) live in cache, so a topK read is an O(k) range query answered in tens of milliseconds.
  • Windows: aggregators keep counts per time window so "trending now" is the top-K over the last window, separate from the all-time list.

Say explicitly that this is a streaming aggregation, not a database sort — the firehose never hits a ORDER BY; it is absorbed by Kafka, counted incrementally in memory, and read from a precomputed top-K. And name the two-level shape — local top-K per shard, merged to global — because it is how you scale counting horizontally. Naming the streaming aggregation and the merge unprompted is a signal the interviewer is listening for.

A top-K streaming architecture diagram showing event producers emitting views to a partitioned Kafka log, per-shard aggregation workers each maintaining a local count-min sketch and a min-heap of its local top-K, a merge step combining the shards' local top-Ks into a global top-K written to a sorted set in a cache, a query service reading the top-K in tens of milliseconds, and aggregators keeping per-time-window counts for trending.

Deep dive: exact top-K with a sorted set

This is the first decision, and it is the right one when the item set is bounded — a game leaderboard with a few million players, or a top-1000 you can afford to keep exactly. Here you keep an exact, ranked structure and never approximate.

The sorted set. A sorted set (a Redis ZSET) keeps items ordered by score: incrementing a player's score is an O(log n) update, and reading the top K is an O(log n + k) range read — so both the write and the top-K read are cheap even with millions of members. Rank and percentile queries ("where am I on the board?") come for free from the structure. You shard by leaderboard (per game, per region, per time period) so each sorted set stays a manageable size, and you cache it so reads never touch a database.

When it stops fitting. The exact approach costs you one ranked-structure update per event and holds every item in memory, which is fine for millions of players but not for tens of millions of distinct videos at a million events a second — that is hundreds of gigabytes and a brutal write rate. The moment the item cardinality and event rate blow past what you can hold exactly, you switch to the approximate approach. Naming where the sorted set stops working is the senior move.

"A game leaderboard has five million players and you need exact ranks — what do you use, and when would you abandon it?" A sorted set: O(log n) score updates and O(log n + k) top-K reads, sharded per leaderboard and cached, with rank queries for free. You abandon exactness when the distinct item count and event rate make holding every counter in memory infeasible — then you go approximate. Name the sorted set, its costs, and the breaking point.

Deep dive: approximate top-K at scale — count-min sketch + heap

This is the decision that decides the round at real scale. When you cannot keep an exact count for every item, you stop trying to remember items and remember only frequencies, cheaply.

Count-min sketch. A count-min sketch is a probabilistic frequency table: a small 2-D array of counters with several hash functions, where incrementing an item bumps one counter per row, and estimating its count takes the minimum across those rows (the min cancels most hash-collision overcounting). It uses sublinear memory — the hundreds of gigabytes an exact hash map would need shrink to hundreds of megabytes — at the price of a small, one-sided error: it can overcount a rare item but never undercounts, which is exactly the right bias for finding heavy hitters. It forgets the items and remembers only counts by hash, which is why it is so small.

The heap. The sketch tells you how often an item has been seen but not which items are currently on top, so you pair it with a min-heap of fixed size K: for each event, estimate the item's count from the sketch, and if it beats the heap's smallest leader, push it in and evict the loser. The heap always holds the current top-K candidates, and because K is tiny this is trivial. Together, sketch-plus-heap gives you a continuously-updated approximate top-K in a few hundred megabytes. In practice you run this as a stream job (Redis supports a count-min sketch and sorted sets natively; a Flink job keeps the sketch and heap as checkpointed in-memory state and replays from Kafka on failure).

"Tens of millions of videos, a billion views a day — give me the top ten without hundreds of gigabytes of counters." A count-min sketch estimates every item's frequency in hundreds of megabytes (it overcounts slightly, never undercounts — right for heavy hitters), and a min-heap of size K tracks the current leaders from those estimates. Accept bounded error for a huge memory win. Name the sketch, the heap, and the one-sided error.

System Design
HLD · Media & analytics · 60 min
Design a top-K / trending system out loud, with follow-ups

Practise on Zynter →

A top-K counting comparison diagram, two cards. Left card exact: a sorted set of item to score kept ranked, O(log n) updates and O(k) top-reads, exact ranks, used for bounded leaderboards, but holds every item and costs hundreds of gigabytes at high cardinality. Right card approximate: a count-min sketch estimating item frequencies in hundreds of megabytes with a small one-sided overcount error, paired with a min-heap of size K holding the current leaders, used for billion-event trending streams.

Deep dive: windows and two-level aggregation

This is the decision that turns "most-viewed ever" into "trending now," and that scales counting past one machine. Both are about when and where you count.

Time windows. "Trending" is inherently a window — the top-K over the last hour, not all time — so aggregators keep counts per window: a tumbling window (fixed, non-overlapping buckets) is simplest, a sliding window (continuously moving) is smoother but heavier, and a decay (older events weigh less) approximates recency without hard buckets. You keep a separate structure per active window and expire old ones. The all-time list and the trending list are two different windows over the same stream.

Two-level aggregation. You cannot count a million events a second on one node, so you shard the stream (Kafka partitions) and have each shard compute its own local top-K (its own sketch and heap). A merge step then combines the shards' local top-Ks into the global top-K. This works because a truly global heavy hitter is almost certainly a local heavy hitter on the shards it appears in, so merging the local top-Ks recovers the global answer without any node ever seeing the whole stream — the same map-reduce shape that makes the whole thing horizontally scalable. For fault tolerance, the stream job checkpoints its in-memory sketch/heap and replays from Kafka after a crash, so a lost node does not lose the counts.

"It is a sliding one-hour trending list and the stream is too big for one node — how do you compute and keep it current?" Keep per-window counts (tumbling/sliding/decay) so 'trending' is the last-hour top-K; shard the stream and compute a local top-K per shard, then merge local top-Ks into the global top-K — a global heavy hitter is a local one somewhere. Checkpoint and replay from Kafka for fault tolerance. Name the window, the local-then-merge, and checkpoint/replay.

System Design
HLD · Media & analytics · 60 min
Defend exact, approximate and windowed top-K out loud

Practise on Zynter →

A top-K windowing and aggregation diagram showing a partitioned event stream fanned across several shard workers that each compute a local top-K, a merge step combining the local top-Ks into a single global top-K, and a time-window panel contrasting a tumbling window, a sliding window and decay for a trending list, with a note that the stream job checkpoints its state and replays from the log for fault tolerance.

The follow-ups that decide the top-K round

Scan the probes from each deep dive and they rhyme. Master these five and you are ready for a version of the question that is not drawn exactly like this one.

  • "Billions of events, tens of millions of items — top ten without exact counters?" → A count-min sketch estimates frequencies in hundreds of MB (overcounts, never undercounts), and a min-heap of K tracks the leaders. (problem solving)
  • "A game leaderboard needs exact ranks — what then?" → A sorted set: O(log n) updates, O(k) top reads, free rank queries, sharded per leaderboard and cached. (technical execution)
  • "Make it 'trending in the last hour' instead of all-time." → Keep per-window counts (tumbling/sliding/decay); the trending list is the last-window top-K, the all-time list is a different window. (communication)
  • "The stream is too big for one node." → Shard it, compute a local top-K per shard, and merge local top-Ks to global — a global heavy hitter is a local one somewhere. (problem solving)
  • "A counting node crashes — do you lose the top-K?" → No: the stream job checkpoints its sketch/heap and replays from the Kafka log, rebuilding state. (technical execution)

Each maps to a debrief dimension. Reading this list teaches you the answers; it does not teach you to deliver them while someone interrupts — that only comes from saying them out loud.

How to prepare: a one-week plan

You do not need fifty systems. You need this one cold and the framework automatic, so the next "design X" is just new nouns in the same five steps.

  • Days 1–2: Learn the five-step framework until it is reflex. Do the top-K system end to end on paper — requirements (exact vs approximate first), estimate, data model, the producers → Kafka → aggregators → merge → top-K store pipeline.
  • Days 3–4: Drill the counting structures: the sorted set for exact leaderboards (costs and breaking point), and the count-min sketch plus min-heap for approximate top-K (why the error is one-sided). Say each tradeoff in one sentence.
  • Days 5–6: Own windows and aggregation — tumbling/sliding/decay, two-level local-then-merge, and checkpoint/replay for fault tolerance. Answer each of the five follow-ups out loud.
  • Day 7: A full timed 60-minute round, narrating the whole way, with someone (or something) interrupting. This is the rep that counts.

This post is the deep walkthrough behind one row of the broader Top 25 system design interview questions — once the top-K design is automatic, the same spine carries the rest of the list.

Frequently asked questions

How do I design a top-K / trending system step by step?

Follow the five-step HLD framework. Clarify requirements first — exact or approximate, all-time or windowed, how big K and the item set are; estimate to show an exact counter for tens of millions of items is hundreds of gigabytes, which forces an approximate approach at scale; define the record/topK/rank API and the event, sorted set, count-min sketch and min-heap model; draw the streaming pipeline (producers → Kafka → per-shard aggregators → merge → top-K store → query); then deep-dive the three decisions that matter — exact top-K with a sorted set, approximate top-K with a count-min sketch plus a heap, and windows with two-level aggregation.

When should a top-K system be exact versus approximate?

Use exact when the item set is bounded and correctness matters — a game leaderboard with a few million players, or a money-ranked list — because a sorted set gives you exact ranks with O(log n) updates and O(k) top reads, and you can afford to hold every item. Switch to approximate when the stream is a firehose over high-cardinality items — tens of millions of distinct videos at a million events a second — because keeping an exact counter for every one is hundreds of gigabytes and a write per event. There, a count-min sketch estimates frequencies in hundreds of megabytes and a min-heap tracks the leaders, trading a small, one-sided error for an enormous memory and throughput win. The first clarifying question in the interview is usually exactly this fork.

How does a count-min sketch find the top-K?

A count-min sketch is a probabilistic frequency table — a small 2-D array of counters with several hash functions. Incrementing an item bumps one counter in each row, and estimating its count takes the minimum of those counters, which cancels most of the overcounting from hash collisions; the structure can overcount a rare item but never undercounts, which is the right bias for finding heavy hitters, and it uses sublinear memory because it forgets the items and remembers only hashed counts. On its own the sketch tells you how often an item was seen but not which items lead, so you pair it with a fixed-size min-heap: for each event you estimate the item's count and, if it beats the heap's smallest member, push it in and evict the loser — the heap always holds the current top-K.

What is the difference between a leaderboard and a trending system?

They share the top-K spine but differ on exactness and time. A leaderboard is usually exact and all-time (or per-season): a bounded set of players ranked by score in a sorted set, where you need precise ranks and percentile queries, so you keep every counter. A trending system is approximate and windowed: it ranks a huge, open-ended stream of items by recent activity, so it uses a count-min sketch plus a heap over a time window (the last hour, not all time) and accepts estimates. In an interview, nailing which one you are being asked for — and that "trending" implies a window and tolerates approximation while a leaderboard usually does not — is half the battle.

How do you scale top-K counting past a single machine?

With two-level aggregation. You cannot count a million events a second on one node, so you shard the stream across Kafka partitions and have each shard maintain its own local counting structure and compute its own local top-K. A merge step then combines the shards' local top-Ks into the global top-K, which works because a truly global heavy hitter is almost always a local heavy hitter on the shards where it appears, so merging local top-Ks recovers the global answer without any node ever processing the whole stream — the same map-reduce shape that makes it horizontally scalable. For fault tolerance, the stream job checkpoints its in-memory sketch and heap and replays from the Kafka log after a crash, so a lost node rebuilds its counts rather than losing them.

How do I practise a system design interview out loud?

Reading a walkthrough is not the same as defending a design while someone interrupts. On Zynter, the HLD track runs live voice rounds modelled on how real system design loops run — you talk through a shared design on the canvas, and a voice interviewer pushes with the follow-ups above one at a time, then gives you a debrief on problem solving, communication, technical execution, and time management. The score is a practice signal, not a hiring decision.

Practise it on Zynter

You can write ORDER BY count DESC. The round is won on exact-vs-approximate and the merge.

Pick the Leaderboard and Top-K round, draw the streaming pipeline on the canvas, and defend it out loud to a voice interviewer that keeps asking "top ten without exact counters?" and "now make it the last hour, across shards." — then read the debrief. Zynter.ai is on-demand mock interviews modelled on real company loops, with 150+ rounds across 20 companies including the HLD system design track (Sep 2026).

Practise the Top-K round →
Browse all interview tracks

Top comments (0)