Problem, as an interviewer would state it: "Build a task runner that batches multiple pushed tasks and runs them all together as MICROTASKS — the whole batch must run before any real, already-scheduled macrotask (like a setTimeout), even one scheduled earlier."
Examples:
setTimeout(() => console.log("macrotask"), 0);
runner.push(() => console.log("microtask 1"));
runner.push(() => console.log("microtask 2"));
// real order: microtask 1, microtask 2, THEN macrotask -- despite setTimeout being called first
Clarifying questions expected:
- Should multiple pushes made synchronously in the same tick be batched into ONE real microtask, or does each get its own separate microtask?
- Does the runner need to guarantee tasks run in the order they were pushed?
- What is the real, defining difference between a microtask and a macrotask that makes this ordering guaranteed?
Code / implementation expected: Yes — real, direct proof against an actual setTimeout(0) scheduled BEFORE the runner's own tasks, confirming the microtask batch genuinely runs first.