Distributed mode and median across worker shards

0
Sign in

You are given a cluster of k worker nodes, numbered 0 to k - 1. Each worker holds its own unsorted slice (shard) of a large integer dataset. A worker can read only its own shard. Workers talk only by passing messages (send(dst, payload) / recv()), and you may assume a barrier between rounds. Worker 0 can act as the coordinator.

Design and implement a distributed algorithm that computes two statistics of the whole dataset:

  1. Mode: the value that occurs most often. If several values tie, return the smallest one.
  2. Median: the middle value of the sorted data. If the total count is even, return the average of the two middle values.

Rules:

  • No single worker may ever receive all the raw data.
  • Minimize network traffic. State how many integers your algorithm sends and how many communication rounds it needs.
  • Shards can be empty, values can be negative, and the dataset can contain many duplicates.

You can simulate the cluster in one process, for example a Cluster(shards: list[list[int]]) class with mode() -> int and median() -> float, as long as every piece of data that moves between workers goes through an explicit message.

Example 1

shards = [[8, 1, 8], [6, 6, 0], [4]]
mode()   -> 6      # 6 and 8 both appear twice; 6 is smaller
median() -> 6.0    # sorted: 0 1 4 6 6 8 8

Example 2

shards = [[10, 3], [], [5, 12]]
mode()   -> 3      # all values appear once; 3 is the smallest
median() -> 7.5    # sorted: 3 5 10 12 -> (5 + 10) / 2

Expect follow-ups on very many distinct values, fewer communication rounds, skewed data, and worker failures.

AnthropicCodingHardLast asked Sep 2026

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.