Design a Leaderboard
🏆 What is a leaderboard? It is the ranked list of players you see at the end of a match or on a game's home screen. Every time a player scores, their total changes and they may move up the list. The game keeps asking two questions: "what is this player's score?" and "who is at the top right now?".
The leaderboard is a short problem with one real idea in it. Storing scores is easy, and so is sorting them once. The hard part is keeping them in order while they keep changing, so that the top of the board is always cheap to read. Most of this post is about that single choice, and about the small bookkeeping rule that makes it correct.
Requirements
"Design a leaderboard for an online game. Players earn points, we need the top K, and an admin can reset a player."
The prompt leaves out what "the top K" returns, what "earn points" means for an existing player, and what a reset does. Each answer changes the code, so ask.
Clarifying Questions
You: When a player earns points, do we get their new total or the points from this match?
Interviewer: The points from this match. Add them to the player's total. A player we have never seen joins the board with those points.
You: What should top(K) return: the player ids, or something else?
Interviewer: For now, the sum of the K highest totals. The game uses it for a "top 10 strength" number. We may want the actual players later.
You: What does a reset do? Does the player stay on the board with zero points?
Interviewer: No, they leave the board completely. If they score again later, they start over from zero like a new player.
You: How do ties work? If two players both have 50, do both count?
Interviewer: Each player counts once. Two players tied at 50 add 100 to the top 2.
You: Which happens more often, score updates or reads of the top?
Interviewer: Both are frequent. Scores change after every match, and the board is shown on every lobby screen. Neither one can be slow.
You: How many players are we talking about? And is this one server or many?
Interviewer: Assume up to ten thousand players in memory, one process, one thread. We can talk about millions later.
The fifth answer drives the whole design. If reads were rare, we would just sort when asked. If updates were rare, we would sort once after each update. Because both are frequent, we need a structure where both are cheap.
💡 Interview tip: When a problem has a read path and a write path, ask which one is hot. "Both" is a real answer, and it rules out the simple designs that make one side free by making the other side expensive.
Final Requirements
add_score(player_id, score)addsscoreto the player's total. A player not on the board joins with a total ofscore.top(k)returns the sum of thekhighest totals on the board. Each player counts once, and ties are counted like any other total.reset(player_id)removes the player from the board. A lateradd_scorefor that player starts again from 0. Resetting a player who is not on the board does nothing.- Both updates and
topare frequent, so neither may cost a full sort of the board. top(k)is only asked forkbetween 1 and the number of players on the board.
Out of scope
- Player names, profiles and match history.
- Showing the board as a list of players with ranks (we add this in Extensibility).
- Daily and weekly boards (Extensibility).
- Millions of players across many servers (Extensibility, and the /system-design/leaderboard post).