Create Task Scheduler With Dependencies
JavaScript
hard
30 mins
Implement a Task Scheduler with Dependencies that executes tasks in the correct order while handling dependencies.
- Each task has a unique ID and can have zero or more dependencies.
- A task cannot execute until all its dependencies have been completed.
- The scheduler should detect circular dependencies and prevent execution in such cases.
- The order of execution should follow topological sorting of the dependency graph.
Example Inputs & Outputs
const scheduler = new TaskSchedulerWithDependencies(); scheduler.addTask("A", ["B", "C"]); scheduler.addTask("B", ["D"]); scheduler.addTask("C", []); scheduler.addTask("D", []); console.log(scheduler.execute()); // Output: ["D", "B", "C", "A"] (One possible valid order) scheduler.addTask("E", ["F"]); scheduler.addTask("F", ["E"]); console.log(scheduler.execute()); // Output: Error: Circular dependency detected!
Constraints & Edge Cases
- Tasks are represented as unique strings.
- Dependencies are also valid task IDs.
- Each task should be executed only once.
- Circular dependencies should be detected and prevented.
- If a task has no dependencies, it should execute immediately.
- Tasks with independent execution orders can have multiple valid outputs.
Companies:
meta
phonepe
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
