Challenge - 5 Problems
Cycle Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of cycle detection using DFS
What is the output of the following Java code that detects a cycle in a directed graph using DFS?
DSA Java
import java.util.*; public class GraphCycle { private final int V; private final List<List<Integer>> adj; public GraphCycle(int V) { this.V = V; adj = new ArrayList<>(); for (int i = 0; i < V; i++) { adj.add(new ArrayList<>()); } } public void addEdge(int u, int v) { adj.get(u).add(v); } private boolean dfs(int node, boolean[] visited, boolean[] recStack) { visited[node] = true; recStack[node] = true; for (int neighbor : adj.get(node)) { if (!visited[neighbor] && dfs(neighbor, visited, recStack)) { return true; } else if (recStack[neighbor]) { return true; } } recStack[node] = false; return false; } public boolean hasCycle() { boolean[] visited = new boolean[V]; boolean[] recStack = new boolean[V]; for (int i = 0; i < V; i++) { if (!visited[i]) { if (dfs(i, visited, recStack)) { return true; } } } return false; } public static void main(String[] args) { GraphCycle graph = new GraphCycle(4); graph.addEdge(0, 1); graph.addEdge(1, 2); graph.addEdge(2, 3); graph.addEdge(3, 1); System.out.println(graph.hasCycle()); } }
Attempts:
2 left
💡 Hint
Trace the DFS recursion and check if any node is revisited in the current recursion stack.
✗ Incorrect
The graph has a cycle: 1 -> 2 -> 3 -> 1. The DFS detects this cycle by tracking nodes in the recursion stack, so the output is true.
❓ Predict Output
intermediateOutput of cycle detection with no cycle
What is the output of the following Java code that detects a cycle in a directed graph with no cycles?
DSA Java
import java.util.*; public class GraphCycle { private final int V; private final List<List<Integer>> adj; public GraphCycle(int V) { this.V = V; adj = new ArrayList<>(); for (int i = 0; i < V; i++) { adj.add(new ArrayList<>()); } } public void addEdge(int u, int v) { adj.get(u).add(v); } private boolean dfs(int node, boolean[] visited, boolean[] recStack) { visited[node] = true; recStack[node] = true; for (int neighbor : adj.get(node)) { if (!visited[neighbor] && dfs(neighbor, visited, recStack)) { return true; } else if (recStack[neighbor]) { return true; } } recStack[node] = false; return false; } public boolean hasCycle() { boolean[] visited = new boolean[V]; boolean[] recStack = new boolean[V]; for (int i = 0; i < V; i++) { if (!visited[i]) { if (dfs(i, visited, recStack)) { return true; } } } return false; } public static void main(String[] args) { GraphCycle graph = new GraphCycle(3); graph.addEdge(0, 1); graph.addEdge(1, 2); System.out.println(graph.hasCycle()); } }
Attempts:
2 left
💡 Hint
Check if any node is revisited in the current recursion stack during DFS.
✗ Incorrect
The graph has no cycle. The DFS completes without finding any back edge, so the output is false.
🔧 Debug
advancedIdentify the error in cycle detection code
What error will the following Java code produce when detecting a cycle in a directed graph?
DSA Java
import java.util.*; public class GraphCycle { private final int V; private final List<List<Integer>> adj; public GraphCycle(int V) { this.V = V; adj = new ArrayList<>(); for (int i = 0; i < V; i++) { // Note the <= here adj.add(new ArrayList<>()); } } public void addEdge(int u, int v) { adj.get(u).add(v); } private boolean dfs(int node, boolean[] visited, boolean[] recStack) { visited[node] = true; recStack[node] = true; for (int neighbor : adj.get(node)) { if (!visited[neighbor] && dfs(neighbor, visited, recStack)) { return true; } else if (recStack[neighbor]) { return true; } } recStack[node] = false; return false; } public boolean hasCycle() { boolean[] visited = new boolean[V]; boolean[] recStack = new boolean[V]; for (int i = 0; i < V; i++) { if (!visited[i]) { if (dfs(i, visited, recStack)) { return true; } } } return false; } public static void main(String[] args) { GraphCycle graph = new GraphCycle(3); graph.addEdge(0, 1); graph.addEdge(1, 2); graph.addEdge(2, 0); System.out.println(graph.hasCycle()); } }
Attempts:
2 left
💡 Hint
Check the loop that initializes adjacency lists and the size of arrays used.
✗ Incorrect
The adjacency list is initialized with V lists but the original code had <= V which caused one extra list. Here fixed to < V. The visited and recStack arrays have size V. Accessing adj.get(V) causes ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedWhy use recursion stack in cycle detection?
In cycle detection for directed graphs using DFS, why do we maintain a recursion stack (recStack) array alongside the visited array?
Attempts:
2 left
💡 Hint
Think about how a cycle is detected during DFS traversal in a directed graph.
✗ Incorrect
The recursion stack tracks nodes in the current DFS path. If we revisit a node in this stack, it means a cycle exists (a back edge).
🚀 Application
expertDetect cycle in a complex directed graph
Given the following directed graph edges, which option correctly shows the output of the cycle detection code?
DSA Java
import java.util.*; public class GraphCycle { private final int V; private final List<List<Integer>> adj; public GraphCycle(int V) { this.V = V; adj = new ArrayList<>(); for (int i = 0; i < V; i++) { adj.add(new ArrayList<>()); } } public void addEdge(int u, int v) { adj.get(u).add(v); } private boolean dfs(int node, boolean[] visited, boolean[] recStack) { visited[node] = true; recStack[node] = true; for (int neighbor : adj.get(node)) { if (!visited[neighbor] && dfs(neighbor, visited, recStack)) { return true; } else if (recStack[neighbor]) { return true; } } recStack[node] = false; return false; } public boolean hasCycle() { boolean[] visited = new boolean[V]; boolean[] recStack = new boolean[V]; for (int i = 0; i < V; i++) { if (!visited[i]) { if (dfs(i, visited, recStack)) { return true; } } } return false; } public static void main(String[] args) { GraphCycle graph = new GraphCycle(6); graph.addEdge(0, 1); graph.addEdge(1, 2); graph.addEdge(2, 3); graph.addEdge(3, 4); graph.addEdge(4, 5); graph.addEdge(5, 3); System.out.println(graph.hasCycle()); } }
Attempts:
2 left
💡 Hint
Look for a cycle involving nodes 3, 4, and 5.
✗ Incorrect
The graph contains a cycle: 3 -> 4 -> 5 -> 3. The DFS detects this cycle and returns true.
