We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Design A Leaderboard

Number: 1176

Difficulty: Medium

Paid? Yes

Companies: Bloomberg, Amazon, Microsoft, Databricks, Wayfair


Problem Description

Design a Leaderboard class that supports three operations:

  1. addScore(playerId, score): Add the score to the given player's score. If the player does not exist, add them with the given score.
  2. top(K): Return the sum of scores of the top K players.
  3. reset(playerId): Reset the score of the specified player to 0.

The leaderboard starts empty.


Key Insights

  • Use a hash table (dictionary) to maintain a mapping from playerId to their cumulative score.
  • For the top(K) query, the simplest approach is to extract scores from the hash table and then compute the sum of the K highest scores.
  • For top(K), one can either sort the scores or use a heap (priority queue) for efficiency.
  • Given that there are at most 1000 operations and playerIds and K are bounded, a simple implementation suffices.

Space and Time Complexity

Time Complexity:

  • addScore: O(1) per call.
  • reset: O(1) per call.
  • top(K): O(n log K) if using a heap or O(n log n) if sorting, where n is the number of players. Space Complexity:
  • O(n) to store the player scores.

Solution

We maintain a dictionary that maps playerId to its current score. When addScore is called, update the player's score in the dictionary. When reset is called, set the player's score to 0 (or remove the player, but setting to 0 works as well since constraints guarantee valid calls). For the top(K) function, obtain all scores from the dictionary and then compute the sum of the top K scores by either sorting the scores in descending order or using a min-heap to efficiently track the K largest scores. This approach is straightforward and works well within the given constraints.


Code Solutions

# Python solution with detailed comments

from heapq import nlargest

class Leaderboard:
    def __init__(self):
        # Dictionary to map playerId to score
        self.scores = {}  # key: playerId, value: total score

    def addScore(self, playerId, score):
        # If player exists, add the score; otherwise, initialize with the score.
        if playerId in self.scores:
            self.scores[playerId] += score
        else:
            self.scores[playerId] = score

    def top(self, K):
        # Retrieve the top K scores using heapq.nlargest for efficiency.
        top_scores = nlargest(K, self.scores.values())
        return sum(top_scores)

    def reset(self, playerId):
        # Reset the player's score to 0.
        self.scores[playerId] = 0

# Example usage:
# leaderboard = Leaderboard()
# leaderboard.addScore(1,73)
# leaderboard.addScore(2,56)
# leaderboard.addScore(3,39)
# leaderboard.addScore(4,51)
# leaderboard.addScore(5,4)
# print(leaderboard.top(1))  # Expected output: 73
# leaderboard.reset(1)
# leaderboard.reset(2)
# leaderboard.addScore(2,51)
# print(leaderboard.top(3))  # Expected output: 141
← Back to All Questions