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_dataappends a batch to that worker. A worker receives many batches over time and they accumulate.find_modereturns 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