Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
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 sequence in order: union(1, 7), union(6, 4), union(0, 2), union(0, 1), union(4, 1), union(2, 2), union(4, 6). How many distinct components remain?
- A. 3
- B. 4
- C. 1
- D. 2
Correct answer: A. 3
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 3 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 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…
- What are the time complexities of P and Q? c int P(int n) { if (n <= 1) return 1; return P(n/2) + P(n/2) + P(n/2) + P(n/2) + n; } int…