S
All challenges
Practice
Home
Blog
Practice
Examples
Feedback
Detect Cycle in Directed Graph
00:00
Start
00:00
Sign in
Problem
Hints
3
Solution
History
Medium
Graph
DFS
Cycle Detection
Coloring
Amazon
Microsoft
Detect if a directed graph has a cycle using DFS and coloring (white=0, gray=1, black=2).
Examples
Example 1
Input →
0
Output →
1→2→0
Note:
cycle → True
Example 2
Input →
0
Output →
1→2, 0→2
Note:
no cycle → False
Example 3
Input →
single vertex self-loop 0
Output →
0 → True
JavaScript
Python
Java (Coming soon)
C (Coming soon)
C++ (Coming soon)
Reset
Run
Submit
Cases
Results