Bird
Raised Fist0

Given the following code snippet implementing the optimized topological sort for position mapping, what is the final pos array for n=3 and conditions = [(1,2),(2,3)]?

easy🧾 Code Trace Q3 of Q15
Topological Sort - Build a Matrix With Conditions
Given the following code snippet implementing the optimized topological sort for position mapping, what is the final pos array for n=3 and conditions = [(1,2),(2,3)]?
A[1, 0, 2]
B[2, 1, 0]
C[0, 1, 2]
D[0, 2, 1]
Step-by-Step Solution
Solution:
  1. Step 1: Initialize In-degree and Queue

    Calculate in-degree: node1=0, node2=1, node3=1; queue starts with [1].
  2. Step 2: Process Nodes in Topological Order

    Pop 1 -> assign pos[1]=0; decrement in-degree of 2 to 0 -> enqueue 2; pop 2 -> assign pos[2]=1; decrement in-degree of 3 to 0 -> enqueue 3; pop 3 -> assign pos[3]=2.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Positions assigned in correct topological order [OK]
Quick Trick: Topological order assigns increasing positions [OK]
Common Mistakes:
MISTAKES
  • Off-by-one indexing
  • Reversing order of assignment
Trap Explanation:
PITFALL
  • Some candidates reverse the order or confuse zero-based indexing.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute topological sort code.
Master "Build a Matrix With Conditions" 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