Compute task order and layered execution
SnowflakeSoftware Engineer · OnsiteLast updated
This question evaluates a candidate's understanding of graph algorithms and dependency resolution, covering concepts such as topological ordering and cycle detection.
You have N tasks labeled 0..N-1 and prerequisite pairs (a, b) meaning task a must complete before task b can start. Return any valid execution order of all tasks, or an empty list if no such order exists. Additionally, return the tasks grouped by levels, where level i contains all tasks that can start only after all tasks in levels < i complete. Describe the algorithm, complexity, and how you detect cycles.
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.