Section 1: Two Commands and a Trap - Problem Understanding & Clarifications
Executive Summary: We read the leaderboard prompt for Rift Runners, a fictional mobile endless-runner from the fictional studio Kestrel Games, and meet the answer most candidates give in five minutes: ZADD to record a score, ZREVRANK to read a rank. At this scale that answer is right, which is what makes it a trap. Seven clarifying questions surface what it never says: a best score is a max, ties need a rule, and a board with prizes is an incentive to cheat. We also meet Kai, whose run we'll follow through every section.
The interviewer reads from a card:
"Design the leaderboard for a mobile game with 50 million daily players. After every run, a player should see their global rank, the top 100, the players just above and below them, and how they stack up against their friends. There's a weekly leaderboard that resets every Monday, and an all-time one."
You've seen this one. Within a minute you're at the whiteboard:
ZADD lb:all-time 1284500 48213377
ZREVRANK lb:all-time 48213377
ZREVRANGE lb:all-time 0 99 WITHSCORES
A Redis sorted set is a skiplist plus a hash table, so insert and rank are both O(log N). Score, rank, top 100. Done.
Here is the uncomfortable part: for one board at 50 million daily players, this answer is right. Section 3 proves one sorted set per board fits on one Redis primary. The trap is stopping there, without saying why it's right or where it stops being right. And the first line hides a bug. Plain ZADD sets the score. Kai's all-time best is 1,350,200, and the run above scored 1,284,500. That command just lowered Kai's best and dropped them down the board for playing.
Key Insight: A leaderboard is a max, an index and an incentive. The five-minute answer builds the index. The Staff answer also finds the max (so retries and replays are harmless), the tie rule (so equal scores have a defensible order) and the incentive (so the top of the board stays real).
Decomposing the Prompt
Before asking anything, break the territory down out loud:
- What it does: Keeps one best score per player per board and answers four questions. Where am I? Who's on top? Who's next to me? How do I compare with my friends?
- Who uses it: Players after every run (the hot path) and in the menu, a rewards service, moderators. And, if there are prizes, people who want to win without playing.
- Why it exists: Retention. A rank is a reason to play one more run, which is also why the top of the board is worth faking.
- How it's used: The player is waiting on the results screen. But the board is not gameplay: if it's down, the game must still play.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson