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)?
- A. 4
- B. 7
- C. 3
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Seven distinct keys are stored in a max-heap in a 0-indexed array. Which statement is correct?
- How many of these statements are correct? (i) Building a heap from n elements by bottom-up sift-down takes O(n) time. (ii) Inserting n…
- How many different max-heaps (as arrays) can be formed from 5 distinct keys?
- Keys 50, 30, 70, 20, 40, 60, 80, 65 are inserted into an empty BST, then 50 is deleted by replacing it with its inorder SUCCESSOR. What is…
- How many of the 7! insertion orders of the keys 1–7 produce the perfectly balanced BST with root 4, children 2 and 6, and leaves 1, 3, 5, 7?
- A binary tree has inorder 9 5 1 7 2 12 8 4 3 11 and postorder 9 1 2 12 7 5 3 11 4 8. What is its preorder?