Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional

Problem: Courses 0–5 have prerequisites: 1 needs 0, 2 needs 1, 3 needs 2, 4 needs 5, 5 needs 4. A course can be taken only after all its prerequisites are finished. How many courses can never be completed?

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

Correct answer: C. 2

Kahn's topological sort removes courses whose prerequisites are done; the leftovers are stuck in or behind a cycle. Counting courses with prerequisites instead answers a different question.

More Coding Interview Problems questions

Exams that ask this topic