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(0, 2), union(1, 3), union(1, 3), union(0, 8), union(5, 0), union(1, 1). How many distinct components remain?

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

Correct answer: B. 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 6 operations with path compression leaves 5 distinct components.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic