Design a Top K / Trending Topics System
hard 45 minCount billions of mentions a day and show the ten topics that are rising right now, in each country and city.
How to approach it
What this question tests, where it gets hard, and how to spend the time.
Read the question
What this problem tests
Can you define the answer?
Popular or rising, posts or people, which window, which place. The formula decides the design.
Can you split a ranking?
Merging partial lists is wrong unless the keys are divided in the right way.
Can you bound memory?
Say the error you accept, size the structure from it, and say when exact would do.
Do you expect opponents?
A trends list is worth faking. Inputs are not to be trusted.
Think
Five questions before you draw
| Ask yourself | Here |
|---|---|
| Who does what? | Millions write posts. Millions read one short list |
| What must never go wrong? | A harmful or faked topic at number one |
| What is the hardest thing? | The true top K when counts are spread and unbounded |
| What breaks first as it grows? | Writes to one table, then memory, then one hot topic |
| What am I giving up? | Exact counts for rare topics, and up to a minute of freshness |
The order of the conversation
Spend the 45 minutes
| Minutes | Do | Leave with | Skip |
|---|---|---|---|
| 0 to 5 | Define trending, windows, places | Freshness target | Personal trends |
| 5 to 10 | Events, distinct topics, lists | What must be split | Network cards |
| 10 to 15 | Read and write interface | Event and list shape | Auth details |
| 15 to 25 | Grow from one table | Two-stage counting | Product names |
| 25 to 40 | Merging, memory, score, abuse | Two decisions with numbers | Proofs |
| 40 to 45 | Failures and recap | A clear last sentence | New features |
Lines you can open with
- 1
Start
"Before I design it, I would like to agree what trending means, because it decides what I must remember."
- 2
Commit
"I will count accounts, not posts, over the last hour, and compare with the same hour on earlier days."
- 3
When stuck
"Let me follow one mention from the post to the list and ask where it is counted."
- 4
Close
"Each topic has one counter, each counter names its best, and readers only ever see finished lists."
Do
- Say why partition key matters for correctness
- Size the sketch from the error
- Offer the exact version and its cost
- Give the freshness budget
Traps
- "Use Redis sorted sets" for 46,300 increments a second and every region
- Listing the contents of a sketch
- Merging top lists of random partitions
- Ignoring fake accounts