Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern
A disjoint-set (union-find with union by rank and path compression) over 7 elements (0 to 6), each initially its own component, runs this sequence in order: union(3, 0), union(0, 5), union(0, 1), union(0, 1), union(1, 5), union(0, 2), union(6, 3). How many distinct components remain?
- A. 4
- B. 3
- C. 1
- D. 2
Correct answer: D. 2
Each union merges two components only if they have different roots (a union on elements already in the same component changes nothing); tracing all 7 operations with path compression leaves 2 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
- Keys 5, 22, 60, 78, 66, 47, 79, 17, 35 are inserted in this order into an empty BST. Using the BST-specific LCA algorithm (walk down from…
- Keys 41, 51, 61 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- A disjoint-set (union-find with union by rank and path compression) over 7 elements (0 to 6), each initially its own component, runs this…
- Build a binary min-heap in place from the array [50, 42, 62, 89, 76, 45, 3, 82] using bottom-up heapify (sift-down from the last internal…
- A directed graph has edges C→B, B→D, A→D, C→A, C→D, A→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle)…
- Directed edges: A→B (6), A→C (6), C→B (-3), B→D (1). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…