Frontend-flavoured problems · senior
Promise task scheduler with concurrency and priorities
A task scheduler keeps a queue of async functions, runs at most limit at once, starts the highest-priority waiting task whenever one finishes, and gives each caller a promise for its own task's result.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Shape: add(task, priority) returns a new Promise; store { task, priority, seq, resolve, reject } in a waiting queue; call next() after every add and every finish.
- next(): while running < limit and the queue is not empty, take the best item (highest priority, then lowest seq for FIFO), running++, run it, and in finally: running--, next().
- Wrap the call in Promise.resolve().then(task) or try/catch so a task that throws synchronously still rejects only its own promise.
- One failing task must not stop the queue; results map back to the right caller.
- mapLimit(items, limit, fn) keeps results in input order by writing into results[index].
- Extras interviewers ask: cancel a waiting task (remove from queue, reject with AbortError), timeouts (AbortSignal.timeout / AbortSignal.any), retries with backoff, pause/resume, and a heap when the queue is large.
- Frontend uses: upload queues (3 at a time), image preloading, progress bars with a concurrency cap, API batching (Zepto), Uber's task scheduler question.
The red pen
Common traps: mistake → fix
Mistake: Promise.all(tasks.map(run)) respects a limit
Fix: it starts everything at once.
Mistake: Results in completion order
Fix: callers expect input order.
Mistake: Forgetting finally
Fix: a rejected task never frees its slot and the queue stalls.
Mistake: Sorting the whole queue on every add
Fix: fine for small queues; mention a heap for large ones.
Easy to hard
Questions interviewers ask, easy to hard
- Run async tasks with at most 3 at a time.
- Return results in input order.
- Add priorities; same priority runs first-in-first-out.
- Add cancel and timeout per task.
- Use it for progress bars or file uploads in the UI.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.