Problem, as an interviewer would state it: "Build a task scheduler that runs queued ASYNC tasks by PRIORITY, not insertion order — a higher-priority task queued later should still run before a lower-priority one that was queued earlier, as long as neither has started yet."
Examples:
scheduler.schedule(lowPriorityTask, 1);
scheduler.schedule(highPriorityTask, 10); // queued SECOND, but should run FIRST
Clarifying questions expected:
- Can an already-RUNNING task be preempted by a higher-priority one arriving later, or does priority only affect the order of tasks that have not started yet?
- What real concurrency limit applies — one task at a time, or several running simultaneously?
- Should two tasks with the SAME priority run in their original insertion order (a stable sort)?
Code / implementation expected: Yes — real, direct, logged proof that tasks queued after an already-started one correctly run in PRIORITY order, not insertion order.