Reject a Task Dependency That Would Create a Cycle
Last updated: November 29, 2026
Quick Overview
Users add 'blocked by' links between tasks one at a time. Implement `add_dependency(blocker, blocked)` that adds the link only if it does not create a circular dependency, and returns whether it was added.
Asana
November 29, 20260
0
0 solved
Users add 'blocked by' links between tasks one at a time. Implement `add_dependency(blocker, blocked)` that adds the link only if it does not create a circular dependency, and returns whether it was added.
Asana prevents circular task dependencies in the UI, so this is a practical warm-up in a phone screen. The interviewer checks that you reduce the question to reachability, pick the right direction to search, and handle self-loops.
What the Interviewer Expects
- Recognizes that adding blocker to blocked creates a cycle exactly when blocked already reaches blocker
- Uses BFS or iterative DFS with a visited set
- Handles a task depending on itself
- Keeps the graph unchanged when rejecting
- States the per-call cost and when it would be too slow
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the path that would form the cycle so the UI can show it?
- If the graph has millions of edges, how could you speed up repeated checks?
- How do you make this check safe when two users add opposite links at the same time?
- How would removal of a dependency work?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Edges point from blocker to blocked. After any accepted call the graph must remain a DAG. Return True if added (adding an existing edge is fine), False if it would form a cycle....
High-Level Design
A new edge u to v closes a cycle iff there is already a path from v back to u. So search forward from blocked and stop as soon as we hit blocker. Searching from the new edge's head usually explore...