KatInterview

Score Tracker with Ranking and Top-K Queries

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designbinary-search-treeorder-statisticsdata-structures

Last updated

Tests the ability to maintain ordered scores with efficient updates and rank lookups using augmented balanced search trees.

Build a score tracking system that processes a sequence of operations on player scores. The system must support three operations:

  1. Update a player's score: Set a player's score to a new value. If the player is new, add them to the system. If they already exist, overwrite their previous score.

  2. Query a player's rank: Return the player's current rank, where rank is defined as one plus the count of players who have a strictly higher score. If the player doesn't exist in the system, return -1.

  3. Retrieve top performers: Return the IDs of up to k highest-scoring players. Order the results by score descending; when multiple players share the same score, order them by ID ascending.

You'll implement a function that processes a list of operations and returns the corresponding outputs. Each operation that modifies state returns None, while queries return their results.

Input Format:

  • operations: A list of operation names: "update_score", "get_rank", or "get_top_k"
  • arguments: A parallel list where each element contains the parameters for the corresponding operation:
    • "update_score": [player_id, score] - both integers
    • "get_rank": [player_id] - single integer
    • "get_top_k": [k] - single integer

Output Format:

Return a list containing one result per operation:

  • "update_score": None
  • "get_rank": an integer rank (or -1 if player not found)
  • "get_top_k": a list of player IDs

Constraints:

  • 1 ≤ number of operations ≤ 100,000
  • Player IDs are unique positive integers
  • Scores can be any integer (positive, negative, or zero)
  • 0 ≤ k ≤ 100,000
  • If k exceeds the number of players, return all players in the correct order
  • If k ≤ 0, return an empty list

Examples

Example 1

Input: score_tracker(['update_score', 'update_score', 'update_score', 'get_rank', 'get_top_k'], [[1, 50], [2, 80], [3, 80], [1], [2]])

Output: [None, None, None, 3, [2, 3]]

Example 2

Input: score_tracker(['update_score', 'get_rank', 'get_rank', 'get_top_k'], [[10, 100], [99], [10], [5]])

Output: [None, -1, 1, [10]]

Example 3

Input: score_tracker(['get_top_k', 'get_rank', 'update_score', 'get_top_k'], [[3], [7], [7, 20], [10]])

Output: [[], -1, None, [7]]

Hints

Hint 1

Think about what data structure lets you quickly find both 'how many elements are smaller than X' and 'give me the first k elements in order'.

Hint 2

A balanced binary search tree with size augmentation can answer order-statistic queries efficiently. Store players ordered by score (descending) and ID (ascending).

Hint 3

Use a tuple key like (-score, player_id) so Python's natural comparison gives you the correct leaderboard order. Maintain a hash map to track each player's current score for quick lookups.