Bird
Raised Fist0

Examine the following DFS-based sequence reconstruction code snippet. Which line contains a subtle bug that causes incorrect results on some inputs?

medium🐞 Bug Identification Q7 of Q15
Topological Sort - Sequence Reconstruction (Unique Topo Order)
Examine the following DFS-based sequence reconstruction code snippet. Which line contains a subtle bug that causes incorrect results on some inputs?
Afor nei in graph[node]: if not dfs(nei): return False
Bvisited[node] = True # marking node as visited before recursion
Cif node in visited: return visited[node]
Dstack.append(node) after visiting neighbors
Step-by-Step Solution
Solution:
  1. Step 1: Understand visited marking

    Marking visited[node] = True before recursion disables cycle detection.
  2. Step 2: Correct approach

    Must mark node as 'visiting' (False) before recursion and 'visited' (True) after to detect cycles.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Incorrect visited marking breaks cycle detection [OK]
Quick Trick: Mark visiting with False, visited with True [OK]
Common Mistakes:
MISTAKES
  • Marking visited too early, missing cycles
Trap Explanation:
PITFALL
  • Candidates confuse visited states, causing infinite recursion or wrong cycle detection.
Interviewer Note:
CONTEXT
  • Tests subtle bug detection in DFS cycle detection logic.
Master "Sequence Reconstruction (Unique Topo Order)" in Topological Sort

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Topological Sort Quizzes