Data Structures & AlgorithmsVery hardProduct company OA

Problem: union–find runs m = 10⁶ operations on n = 10⁶ elements. Version X uses union by rank AND path compression; version Y uses path compression only (always attaching the first root under the second); version Z uses union by rank only. Which bounds on the total time are correct?

  1. A. X: O(m·α(n)); Y: O(m log n) amortised in the worst case; Z: O(m log n), each find O(log n) worst case
  2. B. X, Y and Z are all O(m·α(n)), because path compression alone already gives the inverse-Ackermann bound
  3. C. X: O(m log* n) only; Y and Z: O(m·n) because trees can degenerate into chains
  4. D. X: O(m); Y: O(m log n); Z: O(m) because rank keeps every tree of height at most 2

Correct answer: A. X: O(m·α(n)); Y: O(m log n) amortised in the worst case; Z: O(m log n), each find O(log n) worst case

Rank alone bounds tree height by log₂ n, so every find is O(log n). Path compression alone has a matching worst case of Θ(m log_(1 + m/n) n), i.e. O(m log n) when m ≈ n – not α. Together they give Tarjan's O(m·α(n)), which is ≤ 4 for any practical n (log* n is a weaker but also valid bound). Y and Z cannot become O(n) per operation amortised: compression repairs chains, and rank prevents them. Rank does not bound height by 2.

More Coding Interview Problems questions

Exams that ask this topic