Sum a random sequence of numbers with multiple threads
viaGlassdoor
Problem What is the best way to sum a large sequence of numbers using multiple threads? Asked language-agnostically; reason about correctness, contention, and the speed-up you can realistically expect.
Input / Output
- Input: a large array/sequence of numbers and a thread (or core) count.
- Output: the total sum.
Expected approach
- Partition the input into roughly equal chunks; each worker computes a partial sum over its own chunk with no shared mutable accumulator, then combine the partial sums.
- Map to fork/join or an
ExecutorService+ futures (Java), goroutines + channels, etc. - Avoid a single shared counter (lock contention) and false sharing of adjacent partial-sum slots (pad/space them out).
Discussion points
- Speed-up is bounded by memory bandwidth and Amdahl's law — more threads is not always faster; quantify the overhead of task creation vs work per chunk.
- Floating-point summation is not associative: parallel reduction changes rounding; mention pairwise/Kahan summation if precision matters.
- Time O(n / p) ideal with p workers, plus O(p) combine.
asked …