Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional

Problem: Vertices 1–6 have these directed prerequisites: 1→4, 2→4, 2→5, 3→5, 4→6, 5→6. Among all valid topological orders, the lexicographically smallest one is produced by always taking the smallest-numbered vertex with in-degree zero. What is the second vertex of that order?

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

Correct answer: A. 2

Kahn's algorithm with a min-heap of zero in-degree vertices yields the lexicographically smallest order. A plain FIFO queue instead of a heap can change the second vertex.

More Coding Interview Problems questions

Exams that ask this topic