Design Game Leaderboard (Low-Level Design)
Add points, show the top players and anyone's rank, with ties handled consistently. Focus: Ranking with ties, and keeping reads cheap.
| Difficulty | MEDIUM |
|---|---|
| Design focus | Ranking with ties, and keeping reads cheap |
| Automated grading | 3 public + 8 hidden tests (11 total) |
| Phase 2 follow-up | This week's leaderboard |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
Players gain points over time; their score is the sum.
Rankings are by score, highest first. Players with equal scores share a rank: a player's rank is 1 + the number of players with a strictly higher score. Lists of top players break ties by name, alphabetically.
Functional requirements
- addScore(player, points) adds points (a new player starts at 0).
- score(player) is 0 for unknown players. top(k) returns up to k { player, score } entries.
- rank(player) throws for an unknown player. reset(player) removes the player entirely.
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
class Leaderboard {
addScore(player: string, points: number): void {
throw new Error("Not implemented");
}
score(player: string): number {
throw new Error("Not implemented");
}
top(k: number): { player: string; score: number }[] {
throw new Error("Not implemented");
}
rank(player: string): number {
throw new Error("Not implemented");
}
reset(player: string): void {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 8 hidden edge-case tests in a sandbox:
Top players
var lb = new Leaderboard();
lb.addScore("ann", 50); lb.addScore("bob", 80); lb.addScore("cat", 20);
assertEqual(lb.top(2), [{ player: "bob", score: 80 }, { player: "ann", score: 50 }]);
Scores add up
var lb = new Leaderboard();
lb.addScore("ann", 10); lb.addScore("ann", 15);
assertEqual(lb.score("ann"), 25);
assertEqual(lb.score("nobody"), 0);
Rank
var lb = new Leaderboard();
lb.addScore("a", 10); lb.addScore("b", 30); lb.addScore("c", 20);
assertEqual(lb.rank("a"), 3);
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- The ordering rule (score, then name) is written once: top() and rank() must agree. One comparison function used by both keeps them consistent.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 11 tests, you unlock the change request: This week's leaderboard.
Pair with High-Level System Design
Ready to scale Game Leaderboard across distributed servers? Try the matching HLD simulator problem: Design Real Time Leaderboard.
Relevant low-level design lessons
Ordering with ties
Write one comparison rule that every part of the code agrees on.
Systems that move step by step
Model a system that changes over time with a clear step rule.
From a problem to classes
Turn a problem statement into a first list of classes and methods.
More low-level design problems
Design Rate Limiter
Two rate-limiting algorithms behind one interface, so callers can swap them.
Design Key-Value Store with Expiry
An in-memory store with per-key expiry, later with nested transactions.
Design LRU Cache
A fixed-size cache that throws out the least recently used entry when full, with every operation in constant time.
Design Expense Sharing (Splitwise)
Split shared expenses several ways, track who owes whom, and suggest the fewest payments to settle up.