Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Nine nodes 0..8 start disconnected. Apply these unions in order (a union of two nodes already in one set does nothing): union(0,1), union(2,3), union(1,3), union(4,5), union(6,7), union(5,7), union(3,7). How many connected components remain?

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

Correct answer: B. 2

Seven unions each join two different sets, so the count falls from 9 to 9 - 7 = 2. Node 8 stays alone and the other eight nodes form one set after union(3,7). Forgetting the last union gives the trap answer 3.

More Coding Interview Problems questions

Exams that ask this topic