Compute build order from dependencies
Compute Build Order from Dependencies
You are tasked with determining a valid execution order for a set of tasks, given their dependencies. The dependencies are provided as a directed graph.
Input
The input is a mapping (e.g., a dictionary or hash map) where each key X represents a task, and its associated value is a list of tasks that directly depend on X. This means X must be completed before any task in its corresponding list can begin.
Example:
A: [B, C] implies that tasks B and C depend on A.
B: [D, E] implies that tasks D and E depend on B.
C: [D] implies that task D depends on C.
Task
Implement a function solve(start_task) that generates a valid execution order for all tasks reachable from the start_task. The order must satisfy the following conditions:
- Prerequisite Order: Every task must appear in the output list after all of its direct and indirect prerequisites have been completed.
- Tie-breaking: If multiple tasks are ready to be executed at any given step, prioritize them alphabetically.
- Cycle Detection: If a circular dependency is detected among the tasks, the function should report this condition.
- Complexity Analysis: State the time and space complexity of your solution.
Output
A list of tasks representing a valid execution order, or an indication of a cycle if one is present.
AI interview prep
Core dilemma
The central engineering conflict is to efficiently compute a unique topological sort (due to alphabetical tie-breaking) for tasks reachable from a specific starting node, while robustly detecting cycles in a potentially large and complex dependency graph.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Compute build order from dependencies
Compute Build Order from Dependencies
You are tasked with determining a valid execution order for a set of tasks, given their dependencies. The dependencies are provided as a directed graph.
Input
The input is a mapping (e.g., a dictionary or hash map) where each key X represents a task, and its associated value is a list of tasks that directly depend on X. This means X must be completed before any task in its corresponding list can begin.
Example:
A: [B, C] implies that tasks B and C depend on A.
B: [D, E] implies that tasks D and E depend on B.
C: [D] implies that task D depends on C.
Task
Implement a function solve(start_task) that generates a valid execution order for all tasks reachable from the start_task. The order must satisfy the following conditions:
- Prerequisite Order: Every task must appear in the output list after all of its direct and indirect prerequisites have been completed.
- Tie-breaking: If multiple tasks are ready to be executed at any given step, prioritize them alphabetically.
- Cycle Detection: If a circular dependency is detected among the tasks, the function should report this condition.
- Complexity Analysis: State the time and space complexity of your solution.
Output
A list of tasks representing a valid execution order, or an indication of a cycle if one is present.