Choosing Worker Count When Merge Cost Grows Quadratically
System DesignModel answerLast updated
Pick the worker count that best minimizes total runtime when compute scales as n/k and combining partial results costs k squared, then justify it with analysis and trade-offs.
A batch job over a dataset of size n takes O(n) time on a single machine. If you split the data evenly across k machines, each one needs O(n/k) time for its share. Afterwards, a coordinator combines the k partial results, which costs O(k^2). Treat all other overheads as negligible.
The total runtime is therefore:
T(k) = n/k + k^2
Which of the following values of k gets closest to minimizing the total runtime?
- A) a fixed value around 10
- B) around
log2 n - C) around
sqrt(n) - D) around
n/2
Choose one option and justify it. A strong answer should:
- Find the true optimum of
T(k)and its asymptotic runtime. - Evaluate every option and compare their asymptotic totals.
- Explain why some of the options are traps.
- Discuss how the design could change if the merge step were organized differently.
Hints
Hint 1
Write down T(k) = n/k + k^2 and note that one term shrinks while the other grows as k increases. The best k is roughly where the two terms balance.
Hint 2
Balancing n/k with k^2 gives a cube-root relationship, which is not among the options. So plug each option into both terms and compare the sums asymptotically.
Hint 3
Check what the merge term becomes for k = sqrt(n). Then ask whether a constant k really changes the asymptotic runtime compared with a single machine.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Answer
B) around log2 n is the best of the four choices. The true optimum is a cube root of n, which is not offered, so the task is to find the option with the lowest asymptotic total.
Step 1: Find the true optimum
Model the runtime as
T(k) = n/k + k^2
The first term falls as machines are added; the second rises faster and faster. Treating k as continuous and differentiating:
T'(k) = -n/k^2 + 2k = 0, so k^3 = n/2 and k* = (n/2)^(1/3).
The second derivative 2n/k^3 + 2 is positive, so this is a minimum. Substituting back gives T(k*) = Θ(n^(2/3)).
A calculus-free shortcut is to balance the terms: n/k ≈ k^2 implies k ≈ n^(1/3). This kind of balancing is the quickest way to answer in an interview.
Step 2: Evaluate each option
| Option | k | Compute n/k | Merge k² | Total |
|---|---|---|---|---|
| A | 10 | n/10 | 100 | Θ(n) |
| B | log₂ n | n / log n | log² n | Θ(n / log n) |
| C | √n | √n | n | Θ(n) |
| D | n/2 | 2 | n²/4 | Θ(n²) |
- A: A constant number of machines only reduces the work by a constant factor. The result is still linear, so there is no asymptotic gain.
- B: Compute falls to
n / log n, and the merge termlog² nis negligible by comparison. The total is strictly sublinear. - C: This is the main trap. Compute drops to
√n, but merging now costs(√n)^2 = n. The total is back to linear, no better than a single machine. It overshoots the optimum. - D: The merge cost explodes to quadratic in
n, far worse than not parallelizing.
Sanity check with n = 1,000,000
| k | n/k | k² | Total |
|---|---|---|---|
| 1 | 1,000,000 | 1 | ~1,000,000 |
| 10 | 100,000 | 100 | ~100,100 |
| ~20 (log₂ n) | 50,000 | 400 | ~50,400 |
| ~79 (optimum) | ~12,600 | ~6,300 | ~18,900 |
| 1,000 (√n) | 1,000 | 1,000,000 | ~1,001,000 |
| 500,000 (n/2) | 2 | 2.5×10¹¹ | ~2.5×10¹¹ |
B is clearly the best among the offered choices and sits on the correct side of the optimum, while C is far beyond it.
Why not √n?
√n is the right answer to the classic variant where the merge cost is O(k), since n/k + k is minimized at k = √n. That habit is the common error here. With a quadratic merge cost, always substitute the candidate back into both terms rather than pattern matching to a familiar result.
Trade-offs and practical considerations
- Superlinear coordination cost creates a sweet spot. Past it, adding machines makes the job slower. This is the same intuition behind Amdahl's law and the Universal Scalability Law, where contention and coherence costs cap speedup.
- Fix the merge, not just k. A quadratic merge usually means the coordinator does all-to-all work. A tree-based reduction (combiners in MapReduce, all-reduce in training systems) cuts merge work to about
O(k)total andO(log k)depth. That pushes the optimum to far largerk. - Reduce data before the merge. Local pre-aggregation or combiners shrink what each worker sends, lowering the merge constant.
- Real overheads matter. Startup time, stragglers, network bandwidth, and failure retries add terms not in this model. In practice, measure
T(k)empirically for a few values ofkand pick the knee of the curve. - Cost matters too. Even if more machines still reduce latency slightly, the marginal gain per dollar usually drops well before the mathematical optimum.
Common mistakes
- Picking
√nout of habit from then/k + kversion. - Assuming more machines is always faster and choosing
n/2. - Treating a constant
kas an asymptotic improvement. - Comparing only one of the two terms instead of their sum.
- Not stating that the exact optimum,
(n/2)^(1/3), is not among the choices, and so failing to explain how the best option was selected.
Summary
The exact minimizer is k ≈ (n/2)^(1/3) with runtime Θ(n^(2/3)). Among the listed options, log2 n yields Θ(n / log n), which beats the Θ(n) of options A and C and the Θ(n²) of option D. In a real system, restructuring the merge as a tree would be a stronger improvement than tuning k alone.