KatInterview

Scored Catalog Rotator: Ordered Traversal with Live Updates

Data Structure DesignModel answer
MediumModel answerData Structure Designheaptreemapconcurrency

Last updated

Design an in-memory playlist that always serves the highest-scored unseen item per round, supports live score changes, and is safe for concurrent callers.

Scored Catalog Rotator

You are building an in-memory content rotator. Every item in the catalog has a numeric score, and that score can change at any moment. The rotator hands out items one at a time through a get() call, enforcing a fairness rule: every item must be served exactly once before any item can be served again.

More precisely:

  • Each call to get() returns exactly one item — the one with the highest score among those not yet served in the current round.
  • When every item has been served, the round resets. The very next get() begins a fresh round in which all items are available again.
  • update_score(item_id, score) changes the score of an existing item. If the item_id is new, the item is added to the catalog.

Score updates can arrive at any time, including in the middle of a round.

Constraints and clarifications

  • The full catalog fits in memory on a single machine. Item IDs are unique.
  • No upper bound on catalog size is given; express complexity in terms of n, the number of items in the catalog.
  • You may define reasonable tie-breaking behavior — just state it explicitly.
  • Clarify what happens in each of these cases and code accordingly:
    • A not-yet-served item's score changes mid-round.
    • An already-served item's score changes mid-round.
    • A brand-new item is added mid-round.
    • get() is called on an empty catalog.

Part 1 — Core implementation

Implement:

class CatalogRotator:
    def update_score(self, item_id: str, score: float) -> None: ...
    def get(self) -> str | None: ...

State the time and space complexity of each operation, including the cost of the round-reset that happens inside get() when a round ends.

What your answer should address:

  • Which data structures you chose and why — specifically how they support highest-score extraction, arbitrary score updates, and round rollover together.
  • Explicit rules for ties and for score updates that arrive mid-round.
  • Big-O for update_score, get in the steady case, and get at the moment of rollover.

Part 2 — Concurrent callers

Multiple threads now call get() and update_score() concurrently.

  • Identify which operations are read-modify-write and must execute atomically.
  • Describe where you place locks and at what granularity.
  • Discuss the trade-off between a single coarse lock and finer-grained alternatives.
  • What invariants must hold even under concurrent access?

Hints

Hint 1

Think about what data you need to maintain in two separate states: items still available this round versus items already served. A sorted structure for the available set lets you extract the max efficiently without a full scan.

Hint 2

A heap alone makes score updates expensive (you can't find an arbitrary element in O(log n)). A sorted map keyed by (score, item_id) combined with a plain hash map gives you O(log n) lookup, removal, and insertion together.

Hint 3

For concurrency, list every piece of shared mutable state — the sorted available set, the served set, and the scores map — then ask: which sequences of reads and writes produce an inconsistent result if interrupted? That tells you the minimum critical sections you need to protect.