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(2, 5), union(1, 1), union(2, 6), union(4, 8), union(1, 3), union(1, 6), union(6, 5). How many distinct components remain?
- A. 4
- B. 5
- C. 3
- D. 2
Correct answer: A. 4
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 4 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
- An undirected graph has adjacency lists A:BCDG B:AEF C:A D:AE E:BD F:B G:A. Neighbours are always explored in alphabetical order. Which is…
- Edges A-E, D-E, B-E, A-C, A-B are added one by one to an initially empty graph on vertices {A, B, C, D, E}, using Union-Find (with path…
- In the undirected graph with edges A–B:14, B–C:2, A–D:11, D–E:8, A–F:14, B–G:11, A–G:3, what is the shortest-path distance from A to B?
- Keys 37, 28, 13, 54, 24, 77, 95, 49, 68 are inserted in this order into an empty BST. What is the preorder traversal?
- A min-heap is built by inserting 69, 20, 19, 35, 32, 87, 52, 58 one by one (sift-up on each insert). After one extract-min (move the last…
- A directed weighted graph has edges A→B:3, A→C:5, B→D:5, D→E:9, A→D:5, C→E:-2 (some weights are negative, but there is no negative-weight…