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 code that detects a cycle in a directed graph using DFS?
DSA Python
def dfs(node, graph, visited, rec_stack): visited.add(node) rec_stack.add(node) for neighbor in graph.get(node, []): if neighbor not in visited: if dfs(neighbor, graph, visited, rec_stack): return True elif neighbor in rec_stack: return True rec_stack.remove(node) return False graph = {0: [1], 1: [2], 2: [0, 3], 3: []} visited = set() rec_stack = set() result = any(dfs(node, graph, visited, rec_stack) for node in graph if node not in visited) print(result)
Attempts:
2 left
💡 Hint
Think about whether the graph contains a back edge that forms a cycle.
✗ Incorrect
The graph has a cycle: 0 -> 1 -> 2 -> 0. The DFS detects this cycle and returns True.
❓ Predict Output
intermediateOutput of cycle detection using colors
What is the output of this code that uses colors (0=unvisited, 1=visiting, 2=visited) to detect a cycle in a directed graph?
DSA Python
def dfs(node, graph, color): if color[node] == 1: return True if color[node] == 2: return False color[node] = 1 for neighbor in graph.get(node, []): if dfs(neighbor, graph, color): return True color[node] = 2 return False graph = {0: [1], 1: [2], 2: [3], 3: []} color = {node: 0 for node in graph} result = any(dfs(node, graph, color) for node in graph) print(result)
Attempts:
2 left
💡 Hint
Check if any node is revisited while still being visited (color 1).
✗ Incorrect
The graph is a straight chain with no cycles, so the output is False.
🔧 Debug
advancedIdentify the error in cycle detection code
What error does the following code raise when detecting cycles in a directed graph?
DSA Python
def dfs(node, graph, visited, rec_stack): visited.add(node) rec_stack.add(node) for neighbor in graph[node]: if neighbor not in visited: if dfs(neighbor, graph, visited, rec_stack): return True elif neighbor in rec_stack: return True rec_stack.remove(node) return False graph = {0: [1], 1: [2], 2: [3]} visited = set() rec_stack = set() result = any(dfs(node, graph, visited, rec_stack) for node in range(4) if node not in visited) print(result)
Attempts:
2 left
💡 Hint
Check the nodes used in the loop and keys in the graph dictionary.
✗ Incorrect
Node 3 is not a key in the graph dictionary, so graph[3] raises a KeyError.
🚀 Application
advancedNumber of cycles in a directed graph
Given the graph below, how many distinct cycles does it contain?
DSA Python
graph = {0: [1], 1: [2, 3], 2: [0], 3: [4], 4: [1]}Attempts:
2 left
💡 Hint
Look for all paths that start and end at the same node without repetition.
✗ Incorrect
There are two cycles: 0->1->2->0 and 1->3->4->1.
🧠 Conceptual
expertWhy does DFS detect cycles in directed graphs but not always in undirected graphs?
Which statement best explains why DFS cycle detection differs between directed and undirected graphs?
Attempts:
2 left
💡 Hint
Consider how edges are treated differently in directed vs undirected graphs during DFS.
✗ Incorrect
In directed graphs, a back edge to a node in the recursion stack indicates a cycle. In undirected graphs, edges are bidirectional, so DFS must track the parent node to avoid mistaking the immediate parent edge as a cycle.
