Data Structures & AlgorithmsHardProduct company OA

Union–find with NO union by rank/size and NO path compression: union(a, b) sets parent[find(a)] = find(b). Starting with singletons 0–5, the calls are union(0,1), union(1,2), union(2,3), union(3,4), union(4,5). How many parent links does find(0) then follow?

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

Correct answer: B. 5

Each union attaches the current root of the growing set under the next singleton: 0 → 1, 1 → 2, …, 4 → 5 – a chain. find(0) walks 0 → 1 → 2 → 3 → 4 → 5: 5 links, i.e. Θ(n). Union by size would keep the height ≤ log₂ n; path compression would flatten the chain after one find.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic