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.
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.
id
(string or integer).
score
(integer).
id
in ascending order (so ordering is total and deterministic).
Implement a data structure (or API) supporting these operations:
update(id, delta)
or
updateTo(id, newScore)
(choose one and clearly document it).
id
does not exist, it is created.
remove(id)
removes the element from the leaderboard. If it doesn’t exist, do nothing.
topK(k)
returns the
k
highest-ranked element IDs (or
(id, score)
pairs).
k
exceeds the number of elements, return all elements.
getRank(id)
returns the 1-indexed rank of
id
under the ordering rules.
id
does not exist, return
-1
.
Define clearly what your update operation means (delta vs set), and ensure your design supports all operations correctly.