GATE Computer ScienceModerateGATE CS (PYQ pattern)
Union by rank without path compression is performed (ties: the root of the second argument is attached below the root of the first) on elements 0–9 in this order: union(0,1), union(2,3), union(0,2), union(4,5), union(6,7), union(4,6), union(0,4), union(8,9), union(8,0). What is the largest depth (distance to the root) of any element?
- A. 2
- B. 4
- C. 5
- D. 3
Correct answer: D. 3
The two 8-element trees built from {0..3} and {4..7} have rank 2; joining them gives a rank-3 tree rooted at 0 in which element 7 has depth 3. The final union attaches the rank-1 tree {8,9} below the root, so no node gets deeper than 3. Rank bounds the height by log₂ n.
More Algorithms questions
- Graham scan or Andrew's monotone chain is used on the points (0,0), (4,1), (2,2), (6,3), (3,5), (8,6), (5,7), (1,8), (7,3), (4,4), (2,6)…
- A Fenwick tree (binary indexed tree) over positions 1–100 performs update(i) by visiting i, then i + (i & −i), and so on while the index…
- The Z-array of a string s has Z[i] = length of the longest common prefix of s and s[i..]. (Z[0] is taken as 0.) What is the sum of all Z…
- What is the length of the longest palindromic substring (contiguous) of xabcbaxyzzyxabcdcbay?
- Consider the congruence 46x ≡ 18 (mod 240). How many solutions x does it have in 0…239, and what is the smallest one?
- How many integers in 1…1000 are coprime to 360?