Data Structures & AlgorithmsHardProduct Company SDE · graph round

Problem: Junctions 1..6 form a precedence graph with edges 4 to 1, 3 to 1, 2 to 5 (an edge a to b means a must come before b). Kahn's algorithm, using a min-heap of zero in-degree nodes, produces the lexicographically smallest topological order over nodes 1..5. Which order does it produce?

  1. A. 2 3 4 5 1
  2. B. 4 3 2 1 5
  3. C. 2 3 4 1 5
  4. D. 2 4 3 1 5

Correct answer: C. 2 3 4 1 5

Zero in-degree nodes start as {2,3,4}; the heap pops 2 (releasing 5), then 3, then 4 (releasing 1), and finally the smallest of {1,5} is 1 before 5. The order 2 3 4 5 1 is valid but not lexicographically smallest.

More Coding Interview Problems questions

Exams that ask this topic