Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE
Problem: An MST is built with Kruskal's algorithm on vertices 0–4 and edges: 0-1 (4), 0-2 (1), 1-2 (2), 1-3 (5), 2-3 (8), 3-4 (3), 2-4 (7). What is the total weight of the minimum spanning tree?
- A. 24
- B. 10
- C. 11
- D. 6
Correct answer: C. 11
Kruskal takes 0-2 (1), 1-2 (2), 3-4 (3), skips 0-1 (4, cycle) and takes 1-3 (5): 1 + 2 + 3 + 5 = 11. Taking the four lightest edges without a cycle check gives 10, stopping after three edges leaves a component disconnected (6), and sorting in descending order gives the maximum spanning tree (24).
More Coding Interview Problems questions
- Problem: How many pairs (i, j) with i < j in the array [4, 8, 1, 5, 9, 2, 7, 3, 6] satisfy a[j] − a[i] = 3?
- Problem: How many contiguous subarrays of [7, 2, 9, 4, 1, 8, 3, 5, 6, 10] have a sum divisible by 7?
- Problem: Floyd–Warshall is run on 4 vertices with directed edges 0→1 (3), 1→2 (4), 0→2 (9), 2→3 (1), 3→0 (2), 1→3 (5). Unreachable pairs…
- Problem: On a 6×6 grid, a robot starts at the top-left corner and moves only right or down to the bottom-right corner. The cells (1,2)…
- Problem: Place 4 non-attacking rooks on a 4×4 board (no two share a row or column) such that none is on a forbidden cell. Forbidden cells…
- Problem: In a grid of 0s and 1s, a connected region is a group of 1-cells that are connected when moving to 8 neighbours (up, down, left…