MediumHash TableDesignSortingUpdated Sep 2026

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

scores17325633945154After the five addScore calls, player 1 leads with 73top(1) → 73reset(1), reset(2)addScore(2, 51)top(3) → 51+51+39 = 141The calls from the example, in order
addScore changes one player, reset removes one player, and top reads the K best scores and sums them.

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

Example 1
Input["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]]
Output[null,null,null,null,null,null,73,null,null,null,141]
WhyAfter the five adds the board holds 73, 56, 39, 51 and 4. top(1) is just the single best score, 73. Then players 1 and 2 are reset, leaving 39, 51 and 4. Player 2 comes back with 51, so the board is 51, 39, 51 and 4. top(3) sums the three best: 51 + 51 + 39 = 141.
Example 2
Input["Leaderboard","addScore","addScore","top","reset","top"] [[],[7,10],[7,5],[1],[7],[1]]
Output[null,null,null,15,null,0]
WhyPlayer 7 gets 10, then 5 more, so their score is 15. top(1) returns 15. After reset(7) the board is empty, and top(1) sums nothing, giving 0.

Asked At

2 companies
CompanyDifficulty
BloombergMediumView all Bloomberg questions →
PinterestMediumView all Pinterest questions →

How to Think About It

1

Store scores in a hash map from player id to score. addScore and reset are then O(1).

2

For top(K), the simplest correct approach is to take the K largest values with a heap: O(n log K).

3

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).

4

reset can simply delete the player; a later addScore recreates them from zero.

5

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

scores17325633945154top(3) needs the three largest values: 73, 56 and 51addScore: scores[id] = scores.get(id,0) + sreset: delete scores[id]top: sum of K largest values
addScore and reset touch a single map entry; top scans the values and keeps the K biggest.
ComplexityTime O(1) for addScore and reset, O(nSpace O(n)The map holds one entry per player; top pays for pulling the K largest values out of n scores.

What Trips People Up in Real Interviews

1

Fully sorting all scores on every top call without discussing alternatives.

2

Forgetting that addScore accumulates — it does not overwrite.

3

Keeping a sorted list and doing linear inserts, which makes every update O(n).

4

Not asking which operations dominate. The right structure depends on the read/write mix.

Solution Code

O(1) for addScore and reset, O(n time
import 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)

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

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.