B. It calls dfs on already visited neighbors causing infinite recursion
C. It uses wrong data types for graph
D. It misses the base case for recursion
Solution
Step 1: Check visited condition before recursive call
The code calls dfs(neighbor) only if visited[neighbor] is true, which means already visited.
Step 2: Identify the problem caused
This causes infinite recursion because it revisits nodes already visited instead of skipping them.
Final Answer:
It calls dfs on already visited neighbors causing infinite recursion -> Option B
Quick Check:
Call dfs only if neighbor not visited [OK]
Hint: Call dfs only if neighbor is NOT visited [OK]
Common Mistakes:
Calling dfs on visited nodes
Forgetting to mark visited nodes
Incorrect loop over neighbors
5. You want to find if there is a path between node 0 and node 4 in this graph:
graph = {{1, 2}, {0, 3}, {0, 4}, {1}, {2}}
Which DFS approach correctly determines if node 4 is reachable from node 0?
hard
A. Run DFS from 4, stop and return true if node 0 is found during traversal
B. Run DFS from 0 and count all visited nodes without checking for 4
C. Run DFS from 0, stop and return true if node 4 is found during traversal
D. Run DFS from 0 but skip node 2 to avoid cycles
Solution
Step 1: Understand the goal
We want to check if node 4 is reachable from node 0, so start DFS at 0.
Step 2: Use DFS to find target node
During DFS, if node 4 is found, stop and return true immediately.
Step 3: Evaluate options
Run DFS from 0, stop and return true if node 4 is found during traversal matches this approach. Run DFS from 4, stop and return true if node 0 is found during traversal starts from wrong node. Run DFS from 0 and count all visited nodes without checking for 4 ignores target. Run DFS from 0 but skip node 2 to avoid cycles skips nodes incorrectly.
Final Answer:
Run DFS from 0, stop and return true if node 4 is found during traversal -> Option C
Quick Check:
DFS from start, stop on target found [OK]
Hint: Stop DFS early when target node is found [OK]
Common Mistakes:
Starting DFS from wrong node
Not checking for target during traversal
Skipping nodes that are part of path
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.