Design A Leaderboard

MediumDesignheap

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Implement Leaderboard for a game that tracks players' running scores. - addScore(playerId, score) adds score to the player's total. A player who is not on the board joins with that score. - top(K) returns the sum of the K highest totals currently on the board. - reset(playerId) removes the player from the board (their total goes back to zero). It is only called for players who are on the board. top(K) is only called when at least K players are on the board. A reset player who scores again rejoins as a new entry.

Examples

Input: ["Leaderboard","addScore","addScore","addScore","top","addScore","top","reset","top"] [[],[1,40],[2,15],[3,70],[1],[2,30],[2],[3],[2]]
Output: [null,null,null,null,70,null,115,null,85]
The best single total is 70. Player 2 rises to 45, so the top two are 70 + 45 = 115. After player 3 is removed, the top two are 45 + 40 = 85.
Input: ["Leaderboard","addScore","addScore","top","reset","addScore","top"] [[],[7,5],[7,5],[1],[7],[7,3],[1]]
Output: [null,null,null,10,null,null,3]
Player 7 accumulates 10. After a reset they restart from 3.

Constraints

  • 1 <= playerId, K <= 10^4
  • 1 <= score <= 100
  • `K` is at most the number of players on the board
  • At most 1000 calls in total

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More heap problems