Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional

Problem: A directed graph has vertices 0–6 and arcs: 0→1, 1→2, 2→0, 2→3, 3→4, 4→5, 5→3, 5→6, 6→6. A vertex lies on a cycle if it can reach itself by following at least one arc. How many vertices lie on at least one directed cycle (a self-loop counts as a cycle)?

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

Correct answer: A. 7

Vertices 0, 1, 2 lie on 0→1→2→0, vertices 3, 4, 5 on 3→4→5→3, and 6 on its self-loop, so all 7 qualify; 2→3 and 5→6 only join cycles. Ignoring the self-loop gives 6, counting cycles (SCCs) instead of vertices gives 3, and marking only the endpoints of DFS back edges gives 5.

More Coding Interview Problems questions

Exams that ask this topic