Skip to content
GeeksSmith

Implement Async Scheduler with Max Concurrency

Problem Statement

Implement a `TaskScheduler(concurrency)` class with an `add(task)` method where `task` is a function returning a Promise. The scheduler must ensure that at most `concurrency` tasks execute concurrently. When a task completes, the next queued task must begin immediately.

Target Complexity

Time:

O(1) add and dequeue

Space:

O(N) for queued task closures

Interview Talking Points:
  • Explain why task is passed as a factory `() => Promise` rather than an already instantiated Promise.
  • Explain the importance of `.finally()` to ensure failed tasks do not freeze the concurrency slots.
Solution Editor (JavaScript)
Ready to run
Test Results
Limits active concurrent tasks to max concurrency limit
Test #1
Continues processing queue even if a task rejects
Test #2