Check if adding edge creates cycle in digraph
Company: Amazon
Role: Applied Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates understanding of graph algorithms, reachability, and algorithmic complexity by determining whether adding a directed edge would introduce a cycle in an otherwise acyclic directed graph.
Read the full Amazon Applied Scientist interview experience this question came from
Constraints
- Existing graph is acyclic
Examples
Input: (3, [(0, 1), (1, 2)], (2, 0))
Expected Output: False
Explanation: Creates a cycle.
Input: (3, [(0, 1)], (1, 2))
Expected Output: True
Explanation: Still acyclic.
Hints
- Adding u->v creates a cycle exactly when u is reachable from v before the edge is added.