Implement Task Dependencies and Failure
PerplexitySoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of graph-based data structures and algorithms for modeling bidirectional task dependencies, status tracking, and efficient cascading failure propagation in an in-memory task manager.
Implement a Python in-memory task manager for an AI workflow system.
Each task has a unique task_id and a status. A task may depend on other tasks. If task A depends on task B, then:
Bis a dependency (parent) ofAAis a dependent (child) ofB
You must store both directions of the relationship.
Implement a class with at least the following behavior:
add_task(task_id)
- Create a new task if it does not already exist.
add_dependency(task_id, dependency_id)
- Record that
task_iddepends ondependency_id. - A task can have multiple dependencies and multiple dependents.
fail_task(task_id)
- Mark the given task as failed.
- Then cascade the failure to every task that directly or transitively depends on it.
- For example, if
Cdepends onBandBdepends onA, then failingAmust also failBandC.
get_status(task_id)
- Return the current status of the task.
Your implementation should handle large dependency graphs efficiently. A naive DFS that repeatedly revisits the same nodes may time out on test cases with hundreds of interconnected tasks.
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.