Implement a Dependency-Aware Task Scheduler
Scale AISoftware Engineer · Technical ScreenLast updated
This question evaluates skills in dependency management, deadline-driven scheduling and algorithmic data-structure design, focusing on handling directed acyclic graphs and ordering constraints.
Implement a TaskManager class with two methods:
AddTasks(tasks): add one or more tasks into the system.ConsumeTask(): return and remove the executable task with the earliest deadline.
Each task has the following fields:
id: unique task identifierddl: integer deadline, where a smaller value means an earlier deadlinesubTasks: a list of prerequisite task IDs that must be completed before this task can be consumed
Requirements:
- Support multiple calls to
AddTasks. ConsumeTask()should return the pending task with the smallest deadline among all currently executable tasks.- A task is executable only if all of its prerequisite tasks have already been consumed.
- If no executable task exists, return
NO_TASK. - For this coding problem, assume task IDs are unique and the dependency graph is valid and acyclic.
Explain the data structures you would use and implement both methods efficiently.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.