Data Structures & AlgorithmsHardICPC regional · Product Company SDE

Problem: An undirected graph has vertices 0–7 and edges 0-1, 0-2, 1-2, 2-3, 4-5, 5-6, 6-4, 6-7. How many connected components contain at least one cycle?

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

Correct answer: A. 2

A connected component has a cycle exactly when its edge count is at least its vertex count. Both components here have four vertices and four edges, while counting the cycles themselves answers a different question.

More Coding Interview Problems questions

Exams that ask this topic