Data Structures & AlgorithmsModerateTCS NQT · Product Company SDE

Problem: A DAG has 4 vertices with edges 0→2 and 1→2 and 2→3 (vertices 0, 1 have no incoming arcs). How many distinct topological orders does it have?

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

Correct answer: C. 2

Enumerate all orders and keep those that respect every arc. Counting orders of the vertices that are source-free without checking the arc 2→3 overcounts.

More Coding Interview Problems questions

Exams that ask this topic