Design A Leaderboard - Problem
Design a Leaderboard class for competitive gaming or sports applications.

Your leaderboard must support three key operations:

๐Ÿ† addScore(playerId, score): Add points to a player's total score. If the player doesn't exist, create a new entry.

๐Ÿ“Š top(K): Return the sum of scores from the top K highest-scoring players.

๐Ÿ”„ reset(playerId): Remove a player from the leaderboard (reset their score to 0).

Example: In an esports tournament, players earn points throughout multiple matches. You need to efficiently track rankings and calculate prize pools for top performers.

The leaderboard starts empty, and you're guaranteed that any player being reset was previously added.

Input & Output

example_1.py โ€” Basic Operations
$ Input: leaderboard = Leaderboard() leaderboard.addScore(1, 73) # Player 1: 73 leaderboard.addScore(2, 56) # Player 2: 56 leaderboard.addScore(3, 39) # Player 3: 39 leaderboard.top(2) # Query top 2
โ€บ Output: 129
๐Ÿ’ก Note: Top 2 players are Player 1 (73) and Player 2 (56). Sum = 73 + 56 = 129
example_2.py โ€” Reset Operation
$ Input: leaderboard.reset(1) # Remove Player 1 leaderboard.addScore(2, 51) # Player 2: 56 + 51 = 107 leaderboard.top(3) # Query top 3
โ€บ Output: 146
๐Ÿ’ก Note: After reset, remaining players: Player 2 (107), Player 3 (39). Top 3 sum = 107 + 39 = 146
example_3.py โ€” Edge Case
$ Input: new_board = Leaderboard() new_board.addScore(1, 100) new_board.top(5) # K > number of players
โ€บ Output: 100
๐Ÿ’ก Note: Only 1 player exists but K=5 requested. Return sum of all available players (just 100)

Constraints

  • 1 โ‰ค playerId โ‰ค 105
  • 1 โ‰ค score โ‰ค 105
  • 1 โ‰ค K โ‰ค total number of players
  • At most 1000 operations will be performed
  • It is guaranteed that playerId exists before calling reset

Visualization

Tap to expand
๐Ÿ† Live Tournament Leaderboard SystemHash Table StoragePlayer_A: 1250Player_B: 980Player_C: 1100Player_D: 750Player_E: 890โšก O(1) UpdatesLive Score UpdatePlayer_A +50 points!Updated: 1250 โ†’ 1300Tournament Rankings๐Ÿฅ‡ Player_A: 1300๐Ÿฅˆ Player_C: 1100๐Ÿฅ‰ Player_B: 9804th: Player_E: 890๐Ÿ“Š O(n log n) Sort๐ŸŽฎ Real-World Tournament Scenario:1. Match Updates: Players earn points during gameplay โ†’ Instant O(1) hash table updates2. Live Broadcast: "Show top 3 players" โ†’ Extract & sort all scores once3. Prize Distribution: Calculate prize pool for top 10 โ†’ Sum top K efficiently4. Player Disqualification: Remove cheater โ†’ O(1) hash table deletionโš–๏ธ Trade-off: Fast updates (frequent) vs. Slower queries (occasional)Perfect for: Esports tournaments, live competitions, gaming leaderboards
Understanding the Visualization
1
Player Registration
New players are added to hash table when they first score points
2
Score Updates
During matches, player scores are updated instantly in O(1) time using hash table lookups
3
Live Rankings
When broadcast needs top K players, all scores are extracted and sorted once
4
Player Elimination
Disqualified players are removed from the leaderboard instantly
Key Takeaway
๐ŸŽฏ Key Insight: Use hash tables for lightning-fast score updates during live gameplay, accepting the cost of sorting only when rankings are actually needed for broadcasts or prize calculations.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
43.5K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen