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 JavaScript code that detects a cycle in a directed graph using DFS?
DSA Javascript
function hasCycle(graph) {
const visited = new Set();
const recStack = new Set();
function dfs(node) {
if (recStack.has(node)) return true;
if (visited.has(node)) return false;
visited.add(node);
recStack.add(node);
for (const neighbor of graph[node] || []) {
if (dfs(neighbor)) return true;
}
recStack.delete(node);
return false;
}
for (const node in graph) {
if (dfs(node)) return true;
}
return false;
}
const graph = {
'0': ['1'],
'1': ['2'],
'2': ['0'],
'3': ['4'],
'4': []
};
console.log(hasCycle(graph));Attempts:
2 left
💡 Hint
Think about whether the graph contains a cycle involving nodes 0, 1, and 2.
✗ Incorrect
The graph has a cycle 0 -> 1 -> 2 -> 0, so the function returns true.
❓ Predict Output
intermediateOutput of cycle detection on acyclic graph
What does the following code print when detecting a cycle in this directed graph?
DSA Javascript
function detectCycle(graph) {
const visited = new Set();
const recStack = new Set();
function dfs(node) {
if (recStack.has(node)) return true;
if (visited.has(node)) return false;
visited.add(node);
recStack.add(node);
for (const neighbor of graph[node] || []) {
if (dfs(neighbor)) return true;
}
recStack.delete(node);
return false;
}
for (const node in graph) {
if (dfs(node)) return true;
}
return false;
}
const graph = {
'0': ['1'],
'1': ['2'],
'2': ['3'],
'3': []
};
console.log(detectCycle(graph));Attempts:
2 left
💡 Hint
Check if any node leads back to itself through recursion stack.
✗ Incorrect
The graph is a straight chain with no cycles, so the function returns false.
🔧 Debug
advancedIdentify the error in cycle detection code
What error does the following code produce when run on a graph with a cycle?
DSA Javascript
function checkCycle(graph) {
const visited = new Set();
const recStack = new Set();
function dfs(node) {
if (recStack.has(node)) return true;
if (visited.has(node)) return false;
visited.add(node);
recStack.add(node);
for (const neighbor of graph[node] || []) {
if (dfs(neighbor)) return true;
}
recStack.delete(node);
return false;
}
for (const node in graph) {
if (dfs(node)) return true;
}
return false;
}
const graph = {
'0': ['1'],
'1': ['2'],
'2': ['5', '0'],
'3': []
};
console.log(checkCycle(graph));Attempts:
2 left
💡 Hint
Look at how neighbors are accessed in the for loop.
✗ Incorrect
The code does not handle cases where graph[node] is undefined for a neighbor node. During DFS from '2' to neighbor '5', graph['5'] is undefined, causing TypeError: Cannot read property 'Symbol.iterator' of undefined.
❓ Predict Output
advancedOutput of cycle detection using colors method
What is the output of this code that uses colors (0=unvisited,1=visiting,2=visited) to detect cycles in a directed graph?
DSA Javascript
function detectCycleColors(graph) {
const colors = {};
function dfs(node) {
if (colors[node] === 1) return true;
if (colors[node] === 2) return false;
colors[node] = 1;
for (const neighbor of graph[node] || []) {
if (dfs(neighbor)) return true;
}
colors[node] = 2;
return false;
}
for (const node in graph) {
if (!colors[node]) {
if (dfs(node)) return true;
}
}
return false;
}
const graph = {
'0': ['1'],
'1': ['2'],
'2': ['3'],
'3': ['1']
};
console.log(detectCycleColors(graph));Attempts:
2 left
💡 Hint
Check if the colors method detects the cycle 1 -> 2 -> 3 -> 1.
✗ Incorrect
The cycle exists between nodes 1, 2, and 3, so the function returns true.
🧠 Conceptual
expertWhy is recursion stack needed in DFS cycle detection?
In cycle detection for directed graphs using DFS, why do we need to track nodes in the recursion stack separately from visited nodes?
Attempts:
2 left
💡 Hint
Think about what a back edge means in DFS traversal.
✗ Incorrect
The recursion stack tracks nodes currently being explored. If we revisit a node in this stack, it means a cycle exists (back edge).
