Event Loop Simulator - Problem
Implement a single-threaded event loop simulator that processes tasks from multiple queues with proper priority handling.
Your simulator must handle:
- Task Queue: Regular tasks (like DOM events, I/O callbacks)
- Microtask Queue: High-priority tasks (like Promise.then(), queueMicrotask())
- Timer Queue: Delayed tasks (like setTimeout, setInterval)
The event loop follows JavaScript's execution model:
- Execute all available microtasks first
- Execute one task from the task queue
- Execute all microtasks again
- Check for timer tasks that are ready to run
- Repeat until all queues are empty
Given a list of operations with timestamps, return the execution order of all tasks.
Input & Output
Example 1 — Basic Event Loop
$
Input:
operations = [[0, "task", "A"], [0, "microtask", "B"], [5, "timer", "C", 5]]
›
Output:
["B", "A", "C"]
💡 Note:
At time 0: microtask B executes first (highest priority), then task A. At time 10: timer C is ready and executes.
Example 2 — Multiple Microtasks
$
Input:
operations = [[0, "task", "X"], [0, "microtask", "Y"], [0, "microtask", "Z"]]
›
Output:
["Y", "Z", "X"]
💡 Note:
All microtasks (Y, Z) execute first in order, then task X executes.
Example 3 — Timer Scheduling
$
Input:
operations = [[0, "timer", "P", 0], [5, "task", "Q"]]
›
Output:
["P", "Q"]
💡 Note:
Timer P with 0 delay executes immediately at time 0, task Q executes at time 5.
Constraints
- 1 ≤ operations.length ≤ 1000
- 0 ≤ timestamp ≤ 1000
- 0 ≤ delay ≤ 100
- Each task ID is unique
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code