Design an object-oriented real-time Top-K ranking system.
The system receives score updates for many entities, such as users, drivers, restaurants, or products. Each entity has a unique ID and a numeric score. The system must support:
-
update(entity_id, new_score)
: Insert a new entity or update the score of an existing entity.
-
top_k(k)
: Return the
k
entities with the highest scores, ordered from highest to lowest. Ties should be handled deterministically, for example by sorting by
entity_id
ascending after score descending.
-
remove(entity_id)
: Remove an entity from the ranking.
Requirements and follow-ups:
-
Propose appropriate in-memory data structures, such as a hash map plus a sorted structure.
-
Explain how the design changes if multiple threads call
update
,
remove
, and
top_k
concurrently.
-
Explain how to handle very large input batches where some records may be malformed or fail during processing.
-
Describe the tests you would write for correctness, edge cases, and concurrency.