Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern

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

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

Correct answer: A. 4

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 4 distinct components.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic