Data Structures & AlgorithmsHardIBM · Deloitte pattern

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

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

Correct answer: C. 5

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic