Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern
A disjoint-set (union-find with union by rank and path compression) over 9 elements (0 to 8), each initially its own component, runs this sequence in order: union(0, 2), union(1, 3), union(1, 3), union(0, 8), union(5, 0), union(1, 1). How many distinct components remain?
- A. 4
- B. 5
- C. 6
- D. 3
Correct answer: B. 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 6 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
- Directed edges: A→B (5), A→C (6), C→B (-3), B→D (1). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…
- An undirected connected graph has edges A–B, B–C, C–D, D–E, E–F, F–G, G–H, D–F, F–H. How many articulation points (cut vertices) and how…
- Keys 14, 21, 49, 22, 15, 16, 33, 66 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into a…
- An undirected connected graph has edges A–B, B–C, C–D, D–E, E–F. How many articulation points (cut vertices) and how many bridges does it…
- Keys 50, 60, 70 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Directed edges: A→B (6), A→C (6), C→B (-5), B→D (1). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…