Data Structures & AlgorithmsHardAmazon/Microsoft SDE pattern
A disjoint-set (union-find with union by rank and path compression) over 8 elements (0 to 7), each initially its own component, runs this sequence in order: union(2, 6), union(6, 2), union(1, 1), union(2, 7), union(4, 1). How many distinct components remain?
- A. 3
- B. 4
- C. 6
- D. 5
Correct answer: D. 5
Each union merges two components only if they have different roots (a union on elements already in the same component changes nothing); tracing all 5 operations with path compression leaves 5 distinct components.
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
- Undirected graph edges: D–C, B–C, A–C, A–B, A–D, B–D. Using a BFS 2-colouring, is this graph bipartite?
- Keys 64, 54, 44 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- A binary tree has preorder traversal [23, 2, 94, 67, 57, 46, 81] and inorder traversal [2, 23, 46, 57, 67, 81, 94]. Reconstructing the…
- Build a binary min-heap in place from the array [29, 45, 8, 5, 74, 90, 69, 56, 38] using bottom-up heapify (sift-down from the last…
- Keys 45, 92, 18, 65, 91, 86, 78, 95 are inserted in this order into an empty BST (no rebalancing). What is the height of the tree (edges…
- Keys 55, 50, 19, 76, 39, 98, 34, 63, 58, 69 are inserted via separate chaining (h(k) = k mod 8, new keys appended to the end of the chain)…