Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

A disjoint-set forest uses path compression but NO union by rank/size. What is its amortised cost per operation over m operations on n elements (m ≥ n)?

  1. A. O(α(n)), the same as with union by rank
  2. B. O(1) worst case per operation
  3. C. O(n) amortised, because trees can become chains
  4. D. O(log n) amortised; reaching O(α(n)) needs union by rank as well

Correct answer: D. O(log n) amortised; reaching O(α(n)) needs union by rank as well

Path compression alone gives Θ(m log_(1+m/n) n) total (Tarjan–van Leeuwen), i.e. O(log n) per operation when m ≈ n. The inverse-Ackermann bound α(n) needs both heuristics; a single find can still be O(n) once, but compression makes chains short afterwards.

More Coding Interview Problems questions

Exams that ask this topic