35. Design a Leaderboard (Top-K)
1
The interview prompt2
35.1 What do you ask first?3
35.2 How big is this, really?4
35.3 What are the doors?5
35.4 What do we store?6
35.5 What does the whole system look like?7
35.6 How do sorted sets work for this?8
35.7 How do you handle real-time updates?9
35.8 How do you handle ties?10
35.9 How do you shard a leaderboard?11
35.10 What would you say in the last five minutes?12
Score yourself13
Variations to practise14
Sources15
Code lab35. Design a Leaderboard (Top-K)
Score yourself
3 min read+10 XP
Score yourself
Give yourself 0, 1 or 2 points per row.
| Area | 0 points | 1 point | 2 points |
|---|---|---|---|
| Requirements | Started drawing | Listed top-N and my rank | Fan-out of one event, freshness, ties and exact finals agreed |
| Estimates | None | Numbers, not used later | Numbers that showed write bursts and read volume drive the design |
| API and data | Skipped | API or store, not both | Reads with updated_at; teams, player points, sorted sets and finals separated |
| High-level design | ORDER BY per request | Sorted set, no update plan | Batched rescoring, cached top-K, per-shard rank, final job |
| Deep dive | Hand-waved | Sorted set described | Update bursts, ties or sharding handled with numbers |
| Trade-offs | None named | Named when asked | Raised unprompted (freshness vs load, exact vs approximate rank, shard key) |
| Communication | Long silences | Mostly clear | Thought out loud, checked in, managed time |
12 to 14: strong hire signal. 8 to 11: solid, polish the weak rows. Under 8: redo the round in a week, timer on.
The bar: fresher vs SDE-2
| Fresher | SDE-2 | |
|---|---|---|
| Structure | Sorted set, ZADD and ZREVRANK | Knows costs, top-K merge, ZCOUNT for rank |
| Updates | Update a score when it changes | Notices one event changes millions; batches and indexes player → teams |
| Ties | Not mentioned | Composite score, competition ranking, prize rules |
| Scale | One Redis | Sharded by team, cached top-K, approximate percentiles |
| Money | Uses the live board | Separate exact, audited final job |
Common mistakes
SELECT ... ORDER BY pointson every refresh.- Updating every team on every ball, with no plan for millions of writes per event.
- Ignoring ties, so equal teams jump around between refreshes.
- Paying prizes from the live leaderboard.
- Sharding by score range without discussing hot shards and moving members.
Quick check
Five questions on this chapter. Pick an answer, then check it.
quiz