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?
- A. 1
- B. 5
- C. 2
- 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.
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
- A connected undirected graph has vertex degrees A: 3, B: 4, C: 2, D: 3, E: 2. Does it have an Euler path (using every edge exactly once)…
- Houses in a row hold [3, 2, 7, 10, 1, 9]. No two ADJACENT houses may both be chosen. What is the maximum total? (dp[i] = max(dp[i − 1]…
- The set [3, 9, 7, 3, 11] is split into two subsets so that the absolute difference of their sums is as small as possible (subset-sum DP…
- Digits map to letters A = 1 … Z = 26. In how many ways can the string '2263' be decoded? (dp over prefixes; a pair counts only if it is…
- Keys 50, 30, 20, 10, 40, 70, 60 are inserted into an empty BST. An iterative inorder traversal pushes nodes while walking left and pops to…
- A course plan has prerequisites (a → b means a must come before b): 1→3, 2→3, 3→5, 4→5, 5→6, 2→4, 6→7. Any number of courses with all…