Data Structures & AlgorithmsHardProduct-company MCQ pattern

Union-find on elements 0–7 uses union by size (the smaller tree's root points to the larger tree's root; on a tie the second root points to the first) and NO path compression. After union(0,1), union(2,3), union(4,5), union(6,7), union(0,2), union(4,6), union(0,4), what is the height of the resulting tree (edges on the longest path to the root)?

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

Correct answer: C. 3

Each union joins two equal-sized trees and adds one level: sizes 2 (height 1), 4 (height 2), 8 (height 3); node 7 → 6 → 4 → 0. Union by size keeps the height ≤ log₂ n = 3. Without balancing, a chain of height 7 is possible.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic