A. Missing removing node from visited after recursion
B. Incorrect use of HashSet instead of List
C. Missing adding node to recStack before recursion
D. No bug, code is correct
Solution
Step 1: Check recursion stack usage
Node must be added to recStack before exploring neighbors to detect back edges.
Step 2: Identify missing step
Code adds node to visited but never adds to recStack before recursion, so cycle detection fails.
Final Answer:
Missing adding node to recStack before recursion -> Option C
Quick Check:
recStack update missing = bug [OK]
Hint: Always add node to recursion stack before DFS call [OK]
Common Mistakes:
Forgetting to add node to recursion stack
Removing node from visited incorrectly
Confusing visited and recursion stack roles
5. Given a directed graph represented as adjacency list:
var graph = new Dictionary<int, List<int>> {
{1, new List<int>{2, 3}},
{2, new List<int>{4}},
{3, new List<int>{4}},
{4, new List<int>{5}},
{5, new List<int>{3}}
};
Which nodes form a cycle in this graph?
hard
A. Nodes 3, 4, 5
B. Nodes 1, 2, 4
C. Nodes 2, 3, 5
D. No cycle exists
Solution
Step 1: Trace edges to find cycle
Edges: 3->4, 4->5, 5->3 form a cycle among nodes 3, 4, 5.
Step 2: Confirm cycle presence
Starting at 3, path 3->4->5->3 loops back, confirming cycle.
Final Answer:
Nodes 3, 4, 5 -> Option A
Quick Check:
Cycle nodes = 3,4,5 [OK]
Hint: Follow edges to see if path loops back to start [OK]
Common Mistakes:
Ignoring edge 5->3 that closes cycle
Assuming no cycle because 1 and 2 are acyclic
Confusing cycle nodes with all connected nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.