1244. Design A Leaderboard
Asked at Bloomberg, Pinterest
Difficulty
Medium
Time
O(1) for addScore and reset, O(n
Space
O(n)
Pattern
Hash Table
Companies
2
Problem
Design a class called Leaderboard that keeps track of players and their scores. The leaderboard starts empty. You must support three operations.
addScore(playerId, score) adds score points to that player. If the player is not on the board yet, they join with exactly score points. Scores only ever go up here, so calling it twice adds both amounts.
top(K) returns the sum of the K highest scores currently on the board. It is guaranteed that at least K players exist when this is called. reset(playerId) sets that player's score back to 0, which is the same as removing them from the board; the player is guaranteed to exist. A later addScore for that id starts them fresh from 0.
Three operations on one board
Constraints
- ●
1 <= playerId, K <= 10000 - ●
K is always less than or equal to the number of players currently on the board - ●
1 <= score <= 100 - ●
At most 1000 function calls in total
Examples
["Leaderboard","addScore","addScore","addScore","addScore","addScore","top","reset","reset","addScore","top"]
[[],[1,73],[2,56],[3,39],[4,51],[5,4],[1],[1],[2],[2,51],[3]][null,null,null,null,null,null,73,null,null,null,141]["Leaderboard","addScore","addScore","top","reset","top"]
[[],[7,10],[7,5],[1],[7],[1]][null,null,null,15,null,0]Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Bloomberg | Medium | View all Bloomberg questions → |
| Medium | View all Pinterest questions → |
How to Think About It
Store scores in a hash map from player id to score. addScore and reset are then O(1).
For top(K), the simplest correct approach is to take the K largest values with a heap: O(n log K).
If top is called far more often than updates, maintain a sorted structure (a balanced BST / sorted multiset of scores). Updates become O(log n) (remove old score, insert new) and top(K) is O(K).
reset can simply delete the player; a later addScore recreates them from zero.
Talk through which operation is hot. That trade-off conversation is what the interviewer is really grading.
Optimal Approach
State: scores map.
addScore(id, s): scores[id] = scores.get(id, 0) + s.top(K): return the sum of the K largest values (heap selection).reset(id): delete id from scores.
Time: O(1) for add and reset, O(n log K) for top. Space: O(n).
One map, then pick the K largest
What Trips People Up in Real Interviews
Fully sorting all scores on every top call without discussing alternatives.
Forgetting that addScore accumulates — it does not overwrite.
Keeping a sorted list and doing linear inserts, which makes every update O(n).
Not asking which operations dominate. The right structure depends on the read/write mix.
Solution Code
O(1) for addScore and reset, O(n timeimport heapq
class Leaderboard:
def __init__(self):
self.scores = {}
def addScore(self, playerId, score):
self.scores[playerId] = self.scores.get(playerId, 0) + score
def top(self, K):
return sum(heapq.nlargest(K, self.scores.values()))
def reset(self, playerId):
self.scores.pop(playerId, None)Frequently Asked Questions
What is the Design A Leaderboard problem?
Design A Leaderboard asks you to support three operations: add points to a player's score, return the sum of the top `K` scores, and reset a player's score to zero. It is a small design problem where the discussion about data structure trade-offs matters more than the code.
How do you solve Design A Leaderboard?
The optimal approach runs in O(1) for addScore and reset, O(n time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Design A Leaderboard?
Design A Leaderboard is asked at Bloomberg, Pinterest. It is a medium difficulty problem.
What are common mistakes on Design A Leaderboard?
- Fully sorting all scores on every `top` call without discussing alternatives.
- Forgetting that `addScore` accumulates — it does not overwrite.
- Keeping a sorted list and doing linear inserts, which makes every update `O(n)`.
- Not asking which operations dominate. The right structure depends on the read/write mix.