Bounded Worker Pool
Problem
Our symbolication service receives a list of jobs (each one resolves the frames of a crash against a dSYM or mapping file) and must run them with at most limit in flight at once, return results in the original order, and stop scheduling new work as soon as any job fails while still letting the in-flight ones finish. Implement runPool(tasks, limit) without using a ready-made pool class. Each task is a function returning a value (or a promise in JavaScript). I will then ask what changes if limit should adapt to CPU load, and how you would write a test that proves the concurrency bound.
Examples
Example 1
Input: 6 tasks each sleeping 50 ms, limit = 2
Output: results [r0..r5] in order; total wall time roughly 150 ms (three waves of two); peak concurrency observed is exactly 2.
Example 2
Input: tasks [ok, ok, throws, ok, ok], limit = 2
Output: the call rejects/raises with the error from task 2; tasks 3 and 4 may have started only if they were already in flight when task 2 failed, never afterwards.
Constraints
- Do not use
Promise.allon everything at once,asyncio.Semaphoreas the only mechanism, orThreadPoolExecutor - Results must be indexed by input position, not completion order
- Must be unit-testable: expose a way to observe peak concurrency
What they look for
A shared cursor over the task list, limit worker loops each pulling the next index, a cancellation flag checked before each pull, error propagation with the first error winning, and a clear explanation of why the cursor is safe (single-threaded event loop in Node vs. a lock in Ruby/Go/Python threads).