HardPro challengePythonJavaScriptTypeScript

Task Scheduler

AsyncConcurrencyPatterns

Implement a Scheduler class that limits the number of **concurrently
running** async tasks.

const scheduler = new Scheduler(concurrency);
scheduler.add(taskFn)  // returns a Promise

When more tasks than concurrency are added, the excess tasks are queued
and executed as slots free up.

solve(tasks, concurrency) runs tasks modelled as [delayMs, value] pairs
and returns the order they complete in.

Sample tests

Test #1Concurrency 2: fast1 and slow start, fast2 queued. fast1 finishes → fast2 starts. fast2 finishes then slow.
Input: [[[100,"slow"],[10,"fast1"],[10,"fast2"]],2]
Output: ["fast1","fast2","slow"]
Test #2Single task
Input: [[[5,"x"]],1]
Output: ["x"]
Test #3Concurrency 2, 4 tasks: a+b start. b(10ms)→c starts. c(10ms)→d starts. d finishes at t=30, a finishes at t=50.
Input: [[[50,"a"],[10,"b"],[10,"c"],[10,"d"]],2]
Output: ["b","c","d","a"]
Test #4Concurrency 2: both start immediately, b finishes first
Input: [[[50,"a"],[10,"b"]],2]
Output: ["b","a"]
Test #5Concurrency 1: sequential in add order
Input: [[[10,"a"],[10,"b"],[10,"c"]],1]
Output: ["a","b","c"]