Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
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 contribute nothing. What is the sum of all shortest-path distances d(i, j) over ordered pairs with i ≠ j that are reachable?
- A. 24
- B. 60
- C. 36
- D. 62
Correct answer: B. 60
After Floyd–Warshall every vertex reaches every other (the cycle through 3→0 closes the graph), and the 12 shortest distances sum to 60. Summing only the direct edge weights gives 24, treating the edges as undirected gives 36, and keeping the direct 0→2 edge of 9 instead of 0→1→2 = 7 gives 62.
More Coding Interview Problems questions
- 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…
- Problem: A rooted tree is given by parent array p = [-1, 0, 0, 1, 1, 2, 5, 5, 3] for nodes 0–8 (node 0 is the root). How many nodes are at…
- Problem: A max-heap starts with the values 9, 8, 6, 5, 4, 3, 1 (inserted in that order). Then three times in succession: pop the maximum…
- Problem: The string "abcbaxyzzyxq" is given. What is the minimum number of characters that must be deleted so the remaining string is a…