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 C++ code that detects a cycle in a directed graph using DFS?
DSA C++
/* Graph edges: 0->1, 1->2, 2->0, 2->3 */ #include <iostream> #include <vector> using namespace std; bool dfs(int node, vector<vector<int>>& graph, vector<int>& visited) { visited[node] = 1; // visiting for (int nbr : graph[node]) { if (visited[nbr] == 0) { if (dfs(nbr, graph, visited)) return true; } else if (visited[nbr] == 1) { return true; // cycle found } } visited[node] = 2; // visited return false; } int main() { vector<vector<int>> graph(4); graph[0].push_back(1); graph[1].push_back(2); graph[2].push_back(0); graph[2].push_back(3); vector<int> visited(4, 0); bool hasCycle = false; for (int i = 0; i < 4; i++) { if (visited[i] == 0) { if (dfs(i, graph, visited)) { hasCycle = true; break; } } } cout << (hasCycle ? "Cycle detected" : "No cycle") << endl; return 0; }
Attempts:
2 left
💡 Hint
Think about the edges forming a loop: 0->1->2->0.
✗ Incorrect
The graph has a cycle formed by nodes 0, 1, and 2. The DFS detects this cycle by revisiting a node currently in the recursion stack (visited[node] == 1).
❓ Predict Output
intermediateOutput of cycle detection using Kahn's Algorithm
What is the output of the following C++ code that detects a cycle in a directed graph using Kahn's Algorithm (topological sort)?
DSA C++
#include <iostream> #include <vector> #include <queue> using namespace std; int main() { int n = 4; vector<vector<int>> graph(n); graph[0].push_back(1); graph[1].push_back(2); graph[2].push_back(3); graph[3].push_back(1); // back edge creating cycle vector<int> indegree(n, 0); for (int i = 0; i < n; i++) { for (int nbr : graph[i]) { indegree[nbr]++; } } queue<int> q; for (int i = 0; i < n; i++) { if (indegree[i] == 0) q.push(i); } int count = 0; while (!q.empty()) { int node = q.front(); q.pop(); count++; for (int nbr : graph[node]) { indegree[nbr]--; if (indegree[nbr] == 0) q.push(nbr); } } cout << (count == n ? "No cycle" : "Cycle detected") << endl; return 0; }
Attempts:
2 left
💡 Hint
Kahn's algorithm fails to process all nodes if a cycle exists.
✗ Incorrect
The graph contains a cycle 1->2->3->1. Kahn's algorithm cannot process all nodes, so count < n, indicating a cycle.
🔧 Debug
advancedIdentify the bug in cycle detection DFS code
The following code is intended to detect a cycle in a directed graph using DFS. What is the bug causing incorrect results?
DSA C++
#include <vector> using namespace std; bool dfs(int node, vector<vector<int>>& graph, vector<bool>& visited) { visited[node] = true; for (int nbr : graph[node]) { if (!visited[nbr]) { if (dfs(nbr, graph, visited)) return true; } else { return true; } } return false; } int main() { // graph initialization omitted return 0; }
Attempts:
2 left
💡 Hint
Think about how to detect back edges in DFS for directed graphs.
✗ Incorrect
Using only a boolean visited vector cannot distinguish nodes currently in recursion stack from fully processed nodes, causing false cycle detection or misses.
🧠 Conceptual
advancedWhy does Kahn's Algorithm detect cycles in directed graphs?
Why does Kahn's Algorithm detect cycles in a directed graph?
Attempts:
2 left
💡 Hint
Think about what happens to indegree counts when a cycle is present.
✗ Incorrect
Kahn's Algorithm removes nodes with zero indegree repeatedly. If a cycle exists, some nodes never reach zero indegree, so the algorithm cannot process all nodes.
🚀 Application
expertDetect cycle and print one cycle path in directed graph
Given the following code snippet, what is the output when detecting a cycle and printing one cycle path in a directed graph?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; bool dfs(int node, vector<vector<int>>& graph, vector<int>& visited, vector<int>& path, vector<int>& cycle) { visited[node] = 1; // visiting path.push_back(node); for (int nbr : graph[node]) { if (visited[nbr] == 0) { if (dfs(nbr, graph, visited, path, cycle)) return true; } else if (visited[nbr] == 1) { // cycle found, record cycle path auto it = find(path.begin(), path.end(), nbr); cycle.assign(it, path.end()); return true; } } visited[node] = 2; // visited path.pop_back(); return false; } int main() { vector<vector<int>> graph(5); graph[0].push_back(1); graph[1].push_back(2); graph[2].push_back(3); graph[3].push_back(1); // cycle 1->2->3->1 graph[3].push_back(4); vector<int> visited(5, 0); vector<int> path, cycle; bool found = false; for (int i = 0; i < 5; i++) { if (visited[i] == 0) { if (dfs(i, graph, visited, path, cycle)) { found = true; break; } } } if (found) { for (int node : cycle) cout << node << " "; } else { cout << "No cycle"; } cout << endl; return 0; }
Attempts:
2 left
💡 Hint
The cycle path starts from the first node revisited in the recursion stack.
✗ Incorrect
The cycle detected is 1->2->3->1. The code prints the cycle nodes starting from 1 to 3 in order.
