HardPro challengePythonJavaScriptTypeScript

Async Queue (concurrency limiter)

AsyncDataStructuresPatterns

Build an AsyncQueue(concurrency) class that runs at most
concurrency async tasks in parallel and queues the rest.

add(task) accepts a thunk returning a Promise and returns a Promise that
resolves with the task's value once it actually runs. The queue must:

  • never run more than concurrency tasks at once;
  • resolve each add Promise individually as the task settles;
  • not deadlock if a task throws — failed tasks must release their slot.

The harness's solve returns the **maximum number of in-flight tasks
observed** alongside the ordered results, so the test suite can verify the
concurrency cap.

Sample tests

Test #1Concurrency 1 ⇒ strictly serial
Input: [1,3]
Output: {"results":[0,1,2],"maxActive":1}
Test #2Two tasks, concurrency 2 ⇒ both run together
Input: [2,2]
Output: {"results":[0,1],"maxActive":2}
Test #3TaskCount equals concurrency
Input: [4,4]
Output: {"results":[0,1,2,3],"maxActive":4}
Test #4Concurrency cap holds across batches
Input: [3,10]
Output: {"results":[0,1,2,3,4,5,6,7,8,9],"maxActive":3}
Test #5No tasks ⇒ no work, no peak
Input: [5,0]
Output: {"results":[],"maxActive":0}