KatInterview

Choosing Worker Count When Merge Cost Grows Quadratically

System DesignModel answer
MediumModel answerSystem Designdistributed-systemsscalabilityasymptotic-analysis

Last 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:

  1. Find the true optimum of T(k) and its asymptotic runtime.
  2. Evaluate every option and compare their asymptotic totals.
  3. Explain why some of the options are traps.
  4. 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.