Data Structures & AlgorithmsHardTCS NQT · Infosys pattern

A disjoint-set (union-find with union by rank and path compression) over 8 elements (0 to 7), each initially its own component, runs this sequence in order: union(1, 7), union(6, 4), union(0, 2), union(0, 1), union(4, 1), union(2, 2), union(4, 6). How many distinct components remain?

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

Correct answer: A. 3

Each union merges two components only if they have different roots (a union on elements already in the same component changes nothing); tracing all 7 operations with path compression leaves 3 distinct components.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic