Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: A graph on vertices 0–7 has edges 0-1, 1-2, 2-0, 3-4, 4-5, 5-6, 6-3, 6-7. The edges are removed one at a time in this order: 2-0, 6-3, 4-5. After each removal, the number of connected components is recorded. What is the sum of the component counts recorded over the three removals?

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

Correct answer: B. 7

Removing 2-0 and 6-3 breaks cycles, so the count stays 2 and 2; removing 4-5 then cuts the path 3-…-4 from 5-6-7, giving 3. Sum 2 + 2 + 3 = 7. Recording only the final count gives 3, assuming every removal adds a component gives 3 + 4 + 5 = 12, and missing that 4-5 is now a bridge gives 6.

More Coding Interview Problems questions

Exams that ask this topic