Scores and Top-K Totals on a Leaderboard

Quick Overview

Scores and Top-K Totals on a Leaderboard. Implement Leaderboard, initially empty. addScore(playerId,score) adds to an existing player's score or creates that player with score.

Scores and Top-K Totals on a Leaderboard

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement Leaderboard, initially empty. addScore(playerId,score) adds to an existing player's score or creates that player with score. top(K) returns the sum of the K largest current player scores. reset(playerId) removes that player, equivalently resetting their accumulated score to zero before a later add. The source guarantees the player was previously added before reset. ```cpp class Leaderboard { public: Leaderboard(); void addScore(int playerId, int score); int top(int K); void reset(int playerId); }; ``` ### Examples **Example 1:** ```text Input: construct Leaderboard(); addScore(4,8); addScore(7,6); addScore(4,3); top(2); reset(4); top(1) Output: top results17,6 Explanation: The two scores are11 and6 before reset, then only player7 remains. ``` **Example 2:** ```text Input: construct Leaderboard(); addScore(2,5); reset(2); addScore(2,4); top(1) Output: 4 Explanation: A reset does not carry the earlier5 into the new score. ``` ### Constraints - Both playerId and K are integers in `[1,10000]`. - K is at most the current number of players. - Each added score is an integer from 1 through 100. - At most 1000 function calls occur. - A reset names a player that was previously added. ### Contract considerations Keep the constructor and addScore/top/reset methods; the source guarantees K does not exceed the current player count. ```hint Read the boundary condition After reset and a later addScore for the same ID, do scores from before reset still contribute? ``` ### Boundary reminders Keep the constructor and addScore/top/reset methods; the source guarantees K does not exceed the current player count.

Overview: Scores and Top-K Totals on a Leaderboard. Implement Leaderboard, initially empty. addScore(playerId,score) adds to an existing player's score or creates that player with score.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement Leaderboard, initially empty. addScore(playerId,score) adds to an existing player's score or creates that player with score. top(K) returns the sum of the K largest current player scores. reset(playerId) removes that player, equivalently resetting their accumulated score to zero before a later add. The source guarantees the player was previously added before reset.

class Leaderboard {
public:
    Leaderboard();
    void addScore(int playerId, int score);
    int top(int K);
    void reset(int playerId);
};

Examples

Example 1:

Input: construct Leaderboard(); addScore(4,8); addScore(7,6); addScore(4,3); top(2); reset(4); top(1)
Output: top results17,6
Explanation: The two scores are11 and6 before reset, then only player7 remains.

Example 2:

Input: construct Leaderboard(); addScore(2,5); reset(2); addScore(2,4); top(1)
Output: 4
Explanation: A reset does not carry the earlier5 into the new score.

Constraints

  • Both playerId and K are integers in [1,10000] .
  • K is at most the current number of players.
  • Each added score is an integer from 1 through 100.
  • At most 1000 function calls occur.
  • A reset names a player that was previously added.

Contract considerations

Keep the constructor and addScore/top/reset methods; the source guarantees K does not exceed the current player count.

Boundary reminders

Keep the constructor and addScore/top/reset methods; the source guarantees K does not exceed the current player count.