Design a dynamic leaderboard with rank queries
Company: Anchorage Digital
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem: Dynamic Leaderboard
You are asked to implement a **leaderboard** that maintains scores for a set of elements (e.g., players/items). Scores can change over time, and you must support efficient ranking queries.
### Data model
- Each element has a unique `id` (string or integer).
- Each element has a numeric `score` (integer).
- Higher score means a better rank.
- **Rank 1** is the highest score.
- **Ties:** if multiple elements have the same score, break ties by `id` in ascending order (so ordering is total and deterministic).
### Operations to support
Implement a data structure (or API) supporting these operations:
1. **Update score**
- `update(id, delta)` or `updateTo(id, newScore)` (choose one and clearly document it).
- If `id` does not exist, it is created.
2. **Delete element**
- `remove(id)` removes the element from the leaderboard. If it doesn’t exist, do nothing.
3. **Get top-K elements**
- `topK(k)` returns the `k` highest-ranked element IDs (or `(id, score)` pairs).
- If `k` exceeds the number of elements, return all elements.
4. **Get rank of a specific element**
- `getRank(id)` returns the 1-indexed rank of `id` under the ordering rules.
- If `id` does not exist, return `-1`.
### What to discuss
- The time complexity of each operation.
- How you would optimize for real-world usage patterns (e.g., frequent updates, frequent top-K queries, many deletes).
### Constraints (assume)
- Up to ~1e5 elements.
- Up to ~1e5 operations.
Define clearly what your `update` operation means (delta vs set), and ensure your design supports all operations correctly.
Quick Answer: This question evaluates proficiency with dynamic data structures and order-statistics, including maintaining sorted order under updates, deterministic tie-breaking, and analyzing time complexity for rank and top-K operations.