loopprep

Worker Mode Tracker

total 00:00
Hard50 min Asked at Anthropic
  1. Step 1 · Global mode 00:00
  2. Step 2 Locked
  3. Step 3 Locked
  4. Step 4 Locked
  5. Step 5 Locked

A coordinator collects integers from worker shards and answers global frequency queries. Start with counting, add removal and top-k, then make the hot query O(1) and let the worker pool grow.

Step 1 — Global mode

A coordinator collects integer data from a fixed pool of worker shards and answers questions about the combined data.

class WorkerFrequencyCoordinator:
    def __init__(self, num_workers: int)
    def add_data(self, worker_id: int, data: List[int]) -> None
    def find_mode(self) -> int

Rules:

  • Workers are numbered 0 .. num_workers - 1.
  • add_data appends a batch to that worker. A worker receives many batches over time and they accumulate.
  • find_mode returns the most frequent value across all workers.
  • If several values tie on frequency, return the smallest of them.
  • With no data at all, return -1.
c = WorkerFrequencyCoordinator(4)
c.add_data(0, [9, 2, 9, 4])
c.add_data(3, [2, 2, 5])
c.find_mode()          # 2  — three 2s beat two 9s
c.add_data(0, [9])
c.find_mode()          # 2  — now three each, and 2 wins the tie