Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: Kruskal's algorithm on the edges 0-1 (3), 1-2 (1), 0-2 (2), 2-3 (5), 1-3 (4), 3-4 (2), 2-4 (6) processes the edges in increasing weight order. Which edge is the fourth one added to the spanning tree? Report its weight.
- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
Kruskal adds the cheapest edge that does not close a cycle, so the sequence of accepted weights is nondecreasing. Skipping the cycle test accepts extra heavy edges and changes the fourth value.
More Coding Interview Problems questions
- Problem: A tree has 7 nodes labelled 1–7 with edges 1-2, 1-3, 3-4, 3-5, 5-6, 5-7. Node 1 is the root. What is the depth of the deepest…
- Problem: Strings "axbycz" and "abc" are given. Count the subsequences of "axbycz" that equal "abc" (indices must be increasing, and…
- Problem: Starting with the list [1, 2, 3, 4, 5, 6], repeatedly remove every second element (keeping the first) until one element remains…
- Problem: In the array [5, 1, 4, 2, 8, 0, 2], count the pairs (i, j) with i < j and a[i] > 2·a[j] (so a[i] is more than double a[j]).
- Problem: The string "aabcbbca" is processed by a sliding window that keeps at most one distinct letter 'a' inside (other letters are…
- Problem: How many integers in [1, 200] are divisible by exactly one of 6 and 8?