Design a Top-K Tracker
🔥 What is a top-K tracker? It is the piece of a product that answers "what is popular right now?". A music app shows the songs played most in the last five minutes, a social app shows trending hashtags, a news site shows the most viewed posts. Events stream in, and every few seconds someone asks for the top K of the recent past.
Top songs, trending hashtags and most viewed posts are the same design with different names. An event says "this item happened, from this source, at this time", and a query asks for the K items with the most events in a recent window. We use songs as the running example, and the code never looks at what an item is, so it works for hashtags and posts unchanged. Plays from several sources (web, iOS, Android) are part of the main requirements, and the multi-server version of "multiple sources" comes up in Extensibility.
The problem looks like a one-liner (count, then sort), and interviewers like it for exactly that reason. The interesting parts are what happens when old events must stop counting, how to avoid sorting everything on every query, and how far the design stretches when the stream gets huge.
Requirements
"Design a component that tracks the top K most played songs in the last few minutes."
From there, the questions below decide the design. Each answer becomes a rule in the code and in the practice tests.
Clarifying Questions
You: What is an item, and what counts as one event?
Interviewer: An item is just an id string: a song id, a hashtag, a post id. Each play is one event. No weights for now.
You: What does "the last few minutes" mean exactly? Is it a fixed window, or does each query pick its own?
Interviewer: One fixed window per tracker, given in seconds. A query at second t counts the events of the last window seconds, up to and including t.
You: If two songs have the same count, which one comes first?
Interviewer: Order them by name so the chart is stable. The same input should always give the same list.
You: Do plays come from one place or several?
Interviewer: From the web player, the iOS app and the Android app. We want the overall chart, and also a chart for each source so the mobile team can see their own.
You: Does the count have to be exact, or is an approximation fine?
Interviewer: Exact. Think tens of thousands of distinct songs in a window, not billions. We can talk about bigger scale later.
You: How often is the chart read compared with how often plays arrive?
Interviewer: Plays arrive all the time. The chart is refreshed by a dashboard every few seconds.
You: Where does the time come from?
Interviewer: Pass a timestamp in seconds with every call. It never goes backwards.
The window question matters most. "The last 300 seconds up to now" is a sliding window: every second, the oldest second falls out. That is very different from "this five-minute block" (a tumbling window, which resets to zero at 12:05). Ask which one they mean; the sliding one is the usual answer and the harder one.
💡 Interview tip: Pin down the window boundaries with a number. "An event at second 10 with a window of 10 counts for queries at 10 through 19, and not at 20" is a sentence your interviewer can confirm in two seconds, and it saves you an off-by-one bug later.
Final Requirements
- The tracker is created with
k(how many items a query returns) andwindow(in seconds). record(item, source, timestamp)adds one event foritemfromsource.top(timestamp)returns at mostkitems with the most events among those withtimestamp - window < t <= timestamp, counting every source.topForSource(source, timestamp)does the same with only that source's events. An unknown source returns an empty list.- Items are ordered by count, highest first, then by name. Items with no events in the window are not returned.
- Counts are exact. Timestamps are passed in and never decrease.
Out of scope
- Several windows at once (last minute, hour, day), and all-time charts (we come back to these in Extensibility).
- Approximate counting for huge streams, and merging charts from several servers (also Extensibility).
- Weighting events (a like worth more than a view), personalization and spam filtering.
- Persistence: the counts live in memory and are rebuilt from the stream after a restart.