Data Structures & AlgorithmsHardIBM · Deloitte 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(4, 6), union(6, 4), union(4, 3), union(0, 0), union(4, 3). How many distinct components remain?
- A. 6
- B. 2
- C. 5
- D. 4
Correct answer: C. 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
- 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…
- A directed graph has edges D→B, C→B, B→A, D→C, B→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle), does…
- Directed edges: A→B (5), A→C (6), C→B (-3), B→D (3). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…
- Keys 34, 69, 39, 53, 61, 76, 35 are inserted in this order into an empty BST (no rebalancing). What is the height of the tree (edges on…