Design a leaderboard

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2024-10-19

Problem Statement

Design a Leaderboard that tracks and ranks players based on their scores. The leaderboard should efficiently support score updates, rankings, and querying the top players.

🔹 Ask 1: Simple Use Case

🎯 Functional Requirements

Implement the following methods:

1.addPlayer(String playerId, int score)
•Adds a new player to the leaderboard with the given score.
•If the player already exists, update the score.
2.getTopK(int k)
•Returns a list of the top k players with the highest scores, sorted in descending order of score.
3.getRank(String playerId)
•Returns the 1-based rank of the given player, based on the current scores (1 = highest score).


Solution

Comments