Home
← All questions

Priority async task scheduler in JavaScript

HardPremiumJavaScript

Asked at Google · Amazon · Meta

JavaScript interview practice: Run async tasks by priority with a concurrency limit

Problem

Implement createScheduler({ concurrency }) that exposes schedule(task, priority) where:

  • task is an async function returning a Promise.
  • Higher priority runs before lower when slots are free.
  • At most concurrency tasks run in parallel.
  • Tasks with the same priority run FIFO.
const scheduler = createScheduler({ concurrency: 2 });
scheduler.schedule(() => fetch("/low"), 1);
scheduler.schedule(() => fetch("/high"), 10); // starts before queued low-priority work
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium