~/problems / Pools & pipelines / Thread pool / concurrent crawler

Multithreaded array sort

medium 2 levels ~35 min

Level 1 Sort segments in parallel, then merge

A service wants to speed up sorting by splitting the work across threads. Write

parallel_sort(nums: list[int], k: int, sort_segment, merge_pair=None) -> list[int]

that returns all of nums in ascending order, following these rules (the tests check each one):

  1. Split nums into m = min(k, len(nums)) contiguous segments, as even as possible: sizes differ by at most one, and the longer ones come first. E.g. 10 items, k = 4 → sizes 3, 3, 2, 2.
  2. One thread per segment. Call sort_segment(segment) exactly once for each segment, where segment is a new list holding only that segment's items (a thread may not touch anything outside its own segment). Each call must run in its own thread, not the calling thread, and all m calls must be running at the same time: the test's sort_segment waits until all m threads have arrived before any of them continues.
  3. sort_segment returns the sorted segment (use the returned list; don't assume it sorted in place).
  4. Merge the m sorted segments into the final sorted list (in the calling thread for this level; ignore merge_pair). Aim for O(n log m) for the merge; the tests merge 300,000 items from 64 segments and time it.
  5. If any sort_segment call raises, parallel_sort raises that exception (after the threads have finished).
  6. Empty nums returns [] without calling anything. k >= 1.
parallel_sort([5, 1, 4, 2, 3, 0], 4, sorted)   # [0, 1, 2, 3, 4, 5]
# segments: [5, 1] [4, 2] [3] [0]   (sizes 2, 2, 1, 1)
Show hint

threading.Thread per segment (or a ThreadPoolExecutor(max_workers=m), which does start m threads here), collect results by index, join all, then heapq.merge(*runs). Store exceptions and re-raise after joining.

Level 2 unlocks when level 1 passes.

Topic: Thread pool / concurrent crawler. ThreadPoolExecutor, asyncio, thread-safe visited set.

0:00
Ctrl ' run · Ctrl ↵ submit
esc