Bird
Raised Fist0

Using the optimized topological sort code, what is the output pos array for n=1 and conditions = []?

medium🧾 Code Trace Q4 of Q15
Topological Sort - Build a Matrix With Conditions
Using the optimized topological sort code, what is the output pos array for n=1 and conditions = []?
A[0]
B[1]
C[null]
D[]
Step-by-Step Solution
Solution:
  1. Step 1: Initialize Queue and Position Array

    With n=1 and no conditions, in-degree[1]=0, so queue=[1], pos=[0,0].
  2. Step 2: Process Single Node

    Pop 1 -> assign pos[1]=0; no neighbors to process; return pos[1:] = [0].
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Single node assigned position 0 correctly [OK]
Quick Trick: Single node with no edges -> position 0 [OK]
Common Mistakes:
MISTAKES
  • Returning empty list
  • Assigning position 1 instead of 0
Trap Explanation:
PITFALL
  • Candidates sometimes forget zero-based indexing or return empty for single node.
Interviewer Note:
CONTEXT
  • Tests handling of minimal edge cases in topological sort.
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