Explorer
Node.js

Implement a `createScheduler()` with `add(task, priority)` and `run()`. Higher priority tasks run first.

Problem Statement

Implement a `createScheduler()` with `add(task, priority)` and `run()`. Higher priority tasks run first.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: A priority queue processes items by priority, not insertion order. šŸ’” Hint 2: Sort tasks by priority (higher first) before running. šŸ’” Hint 3: Store tasks with priorities, sort on run(), and execute in order. āœ… Solution: ```javascript function createScheduler() { const tasks = []; return { add(task, priority) { tasks.push({ task, priority }); }, run() { tasks.sort((a, b) => b.priority - a.priority); return tasks.map(t => t.task()); } }; } ```

Task Scheduler (Priority Queue)

Hard
Implement a `createScheduler()` with `add(task, priority)` and `run()`. Higher priority tasks run first.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
["high","mid","low"]