Challenge - 5 Problems
Cycle Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of cycle detection using DFS
What is the output of the following C code that detects a cycle in a directed graph using DFS?
DSA C
#include <stdio.h> #include <stdbool.h> #define V 4 bool dfs(int v, bool graph[V][V], bool visited[V], bool recStack[V]) { if(!visited[v]) { visited[v] = true; recStack[v] = true; for(int i = 0; i < V; i++) { if(graph[v][i]) { if(!visited[i] && dfs(i, graph, visited, recStack)) return true; else if(recStack[i]) return true; } } } recStack[v] = false; return false; } bool isCyclic(bool graph[V][V]) { bool visited[V] = {false}; bool recStack[V] = {false}; for(int i = 0; i < V; i++) { if(dfs(i, graph, visited, recStack)) return true; } return false; } int main() { bool graph[V][V] = { {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}, {1, 0, 0, 0} }; if(isCyclic(graph)) printf("Cycle detected\n"); else printf("No cycle detected\n"); return 0; }
Attempts:
2 left
💡 Hint
Trace the DFS calls and check if any node is revisited in the current recursion stack.
✗ Incorrect
The graph has edges 0->1, 1->2, 2->3, and 3->0 forming a cycle. The DFS detects this cycle by checking the recursion stack.
🧠 Conceptual
intermediate1:30remaining
Understanding recursion stack in cycle detection
In cycle detection for a directed graph using DFS, what is the purpose of the recursion stack (recStack) array?
Attempts:
2 left
💡 Hint
Think about how a cycle is detected during DFS traversal.
✗ Incorrect
The recursion stack marks nodes currently being explored. If a node is revisited while still in the recursion stack, it means a cycle exists.
🔧 Debug
advanced2:00remaining
Identify the error in cycle detection code
What error will occur when running this modified cycle detection code?
DSA C
#include <stdio.h> #include <stdbool.h> #define V 3 bool dfs(int v, bool graph[V][V], bool visited[V], bool recStack[V]) { visited[v] = true; recStack[v] = true; for(int i = 0; i < V; i++) { if(graph[v][i]) { if(!visited[i] && dfs(i, graph, visited, recStack)) return true; else if(recStack[i]) return true; } } recStack[v] = false; return false; } bool isCyclic(bool graph[V][V]) { bool visited[V] = {false}; bool recStack[V] = {false}; for(int i = 0; i < V; i++) { if(dfs(i, graph, visited, recStack)) return true; } return false; } int main() { bool graph[V][V] = { {0, 1, 0}, {0, 0, 1}, {0, 0, 0} }; if(isCyclic(graph)) printf("Cycle detected\n"); else printf("No cycle detected\n"); return 0; }
Attempts:
2 left
💡 Hint
Check the loop bounds in isCyclic function.
✗ Incorrect
The loop in isCyclic runs from 0 to V inclusive, causing an out-of-bounds access when i == V.
🚀 Application
advanced1:30remaining
Detect cycle in a graph with multiple disconnected components
Given a directed graph with 5 nodes and edges: 0->1, 1->2, 3->4, 4->3, which statement about cycle detection is true?
Attempts:
2 left
💡 Hint
Check edges in each disconnected component separately.
✗ Incorrect
Nodes 3 and 4 form a cycle (3->4 and 4->3). The other component 0->1->2 has no cycle.
🧠 Conceptual
expert1:30remaining
Why can't BFS alone detect cycles in directed graphs reliably?
Why is BFS not typically used alone to detect cycles in directed graphs?
Attempts:
2 left
💡 Hint
Think about how cycle detection relies on back edges and recursion stack.
✗ Incorrect
Cycle detection in directed graphs depends on detecting back edges during DFS. BFS does not track recursion paths and thus cannot reliably detect cycles alone.