Data Structures & AlgorithmsVery hardProduct-company MCQ 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(5, 6), union(2, 0), union(0, 2), union(3, 4), union(4, 4), union(1, 6), union(6, 4). How many distinct components remain?
- A. 3
- B. 1
- C. 2
- D. 4
Correct answer: C. 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
- 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…
- A directed graph has edges B→E, E→A, E→C, B→A, E→D, D→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle)…
- What are the time complexities of P and Q? c int P(int n) { if (n <= 1) return 1; return return P(n/2) + n; } int Q(int n) { if (n <= 1)…
- A directed graph has edges E→A, D→A, D→E, B→D, D→C. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a cycle), does…
- Keys 49, 39, 29 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- An undirected connected graph has edges A–B, B–C, C–D, D–E, E–F, F–G, G–H, C–G, A–C. How many articulation points (cut vertices) and how…