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
Coding & Algorithms
Software Engineer
Asana
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
graph reachability
BFS
cycle detection
directed graphs
incremental checks
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported

Related Questions