Design a Parallel Merge Sort
🧩 What is a parallel merge sort? Merge sort splits an array in half, sorts each half and merges the two sorted halves. The halves do not touch each other, so different threads can sort them at the same time. A parallel merge sort does exactly that, on a pool of worker threads, and falls back to a plain sequential sort once a piece is small.
Interviewers ask this question in a few forms: "sort a huge array with multiple threads", "sum a big array in parallel", or "design a small fork/join framework". They are one design. You fork (split the work into independent tasks), run the tasks on a pool, and join (wait for the results and combine them). Sorting combines with a merge, summing combines with +. We build one engine that does both, and we spend real time on the question that always comes at the end: when does splitting stop paying off?
The code is short. What the interviewer is really checking is whether you know the traps: making a new thread for every split, deadlocking a fixed pool with tasks that wait on other tasks, and believing that more threads always means more speed.
Requirements
"Sort a large array of integers using multiple threads. The same engine should also be able to sum an array in parallel."
Clarifying Questions
You: How many threads can we use? Do we create them per call?
Interviewer: You get a thread count up front, and the sorter is reused for many calls. Don't start new threads on every call.
You: When should we stop splitting and just sort sequentially?
Interviewer: Take a threshold in the constructor. A range of at most that many elements is sorted on one thread. Split longer ranges exactly in the middle.
You: Should we sort in place, or return a new array?
Interviewer: Return the sorted array. What happens to the input is up to you.
You: Must it work with a single thread, or a pool much smaller than the number of pieces?
Interviewer: Yes. Any thread count, any input size, and it must never hang.
You: Is the sum the only other operation, or do you expect more?
Interviewer: Sum for now. If your design makes "min" or "count" easy later, even better.
You: Do we need to report anything about how the work was split?
Interviewer: Yes, for testing: after each call, report how many chunks it used.
The fourth answer is the one that shapes the design. "Any thread count, never hang" rules out the most natural recursive version on a plain pool, as we will see. The midpoint rule and the chunk count make the split deterministic, so we can test the splitting even though the thread schedule is random.
💡 Interview tip: Ask whether the engine is reused across calls. If it is, the threads belong to the object (a pool created once), not to each call. That single answer moves you from "spawn threads" to "submit tasks", which is the design the interviewer wants to talk about.
Final Requirements
ParallelSorter(threads, threshold)starts a pool ofthreadsworker threads that is reused by every call.- A range
[lo, hi)with at mostthresholdelements is one chunk and is processed sequentially. A longer range is split atmid = (lo + hi) // 2, and each half is split by the same rule. sort(nums)returns the numbers in ascending order, using the pool to sort chunks and merge results.sum(nums)returns the total, using the same splitting.chunks()returns the number of chunks the lastsortorsumused.- It never deadlocks, for any thread count (including 1) and any input size (including 0).
shutdown()stops the worker threads.
Out of scope
- Data that does not fit in memory (we come back to this in Extensibility).
- Sorting objects with a custom comparator or stability guarantees.
- Several callers using the same sorter at the same moment (we discuss what would break).