Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE
Problem: Edges of an undirected graph on vertices 0–4 are processed in the given order: e1 = 0-1, e2 = 1-2, e3 = 2-3, e4 = 3-4, e5 = 4-0, e6 = 1-3. Using union-find, the first edge that joins two vertices already in the same component is the first cycle-closing edge. Which edge number (1-based) is it?
- A. 5
- B. 6
- C. 4
- D. 7
Correct answer: A. 5
Union-find reports a cycle as soon as an edge's endpoints already share a root. The first four edges form a path, and the fifth edge closes the ring 0-1-2-3-4.
More Coding Interview Problems questions
- Problem: How many spanning trees does the cycle graph C5 (5 vertices in one cycle) have?
- Problem: A directed graph on vertices 0–5 has arcs 0→1, 1→2, 2→0, 2→3, 3→4, 4→3, 4→5, 5→1. How many vertices have in-degree equal to…
- Problem: On the undirected graph with edges 0-1 (4), 0-2 (1), 2-1 (2), 1-3 (5), 2-3 (9), 3-4 (3), find the minimum number of edges used by…
- Problem: The rooted tree (root 1) has children 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 6: [8]. How many nodes have a depth divisible by 3…
- Problem: In the string "abcbaxyzzyx", count the palindromic substrings of length exactly 5.
- Problem: How many adjacent positions i (0 ≤ i < n−1) of "abbaabab" form the pair "ab" or the pair "ba"?