 All Problems
Detect Cycle in Directed Graph
medium
topological sort
graph
dfs
amazon
google
microsoft
oracle

Given a directed graph with V vertices (0 to V-1) and E edges, detect if the graph contains a cycle.

Example 1:

Input: V=4, edges=[[0,1],[1,2],[2,0],[2,3]]
Output: true

Example 2:

Input: V=4, edges=[[0,1],[1,2],[2,3]]
Output: false

Constraints:

  • 1 <= V <= 10^4
  • 0 <= E <= 10^5
Run to check your code against the sample cases, or submit to run every case