Coinbase: Live leaderboard: kth-highest score with score updates and user removal
Problem. Scores arrive continuously. Support these operations:
- Add a score for a user. Re-adding an existing user counts as an update.
- Return the current kth-highest score.
- Remove a user.
Agreed assumptions. k is fixed. Ties need no special ranking, so just return the score value.
Candidate's design. Two ordered sets: one holds the top k scores and the other holds the rest. After each insert or delete, rebalance by moving the boundary element between the sets. The answer is the smallest element of the top-k set, and each operation is O(log n).
Follow-up. What happens when the same user submits repeatedly? Without a user-to-current-score map, old entries stay behind. The fix is to look up and remove the old score before inserting the new one. The candidate missed this until the interviewer asked.
What the interviewer cared about. Correct state handling under updates and deletes. One test the candidate wrote removed a user sitting exactly on the top-k boundary.
Comments 0
No comments yet. Ask the author something, or add what happened in your own round.