Bird
Raised Fist0

Given the following code implementing Kahn's Algorithm for course scheduling, what is the final output when calling findOrder(4, [[1,0],[2,0],[3,1],[3,2]])?

easy🧾 Code Trace Q12 of Q15
Topological Sort - Course Schedule II (Topological Order)
Given the following code implementing Kahn's Algorithm for course scheduling, what is the final output when calling findOrder(4, [[1,0],[2,0],[3,1],[3,2]])?
A[0, 1, 2, 3]
B[]
C[3, 1, 2, 0]
D[0, 2, 1, 3]
Step-by-Step Solution
Solution:
  1. Step 1: Initialize in-degree and queue

    In-degree: [0,1,1,2]; queue starts with [0] since only course 0 has in-degree 0.
  2. Step 2: Process queue and update order

    Pop 0 -> order=[0]; neighbors 1 and 2 in-degree decremented to 0 -> queue=[1,2]; pop 1 -> order=[0,1]; neighbor 3 in-degree decremented to 1; pop 2 -> order=[0,1,2]; neighbor 3 in-degree decremented to 0 -> queue=[3]; pop 3 -> order=[0,1,2,3].
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Order respects prerequisites and includes all courses [OK]
Quick Trick: Track in-degree updates and queue contents carefully [OK]
Common Mistakes:
MISTAKES
  • Mixing order of nodes popped from queue
  • Forgetting to decrement in-degree
  • Returning empty list incorrectly
Trap Explanation:
PITFALL
  • Candidates often confuse the order of nodes with same in-degree zero or miss decrementing in-degree properly.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute BFS topological sort code and track state.
Master "Course Schedule II (Topological 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