Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
A Floyd–Warshall implementation writes its loops as for i … for j … for k (k innermost) instead of k outermost, and runs the triple loop once. What happens in general?
- A. It still gives correct results, only slower
- B. It still gives correct results because min() is commutative
- C. It always returns the correct result but may report false negative cycles
- D. Some distances can stay larger than the true shortest paths, because dist[i][k] or dist[k][j] may be used before it has been finalised
Correct answer: D. Some distances can stay larger than the true shortest paths, because dist[i][k] or dist[k][j] may be used before it has been finalised
Correctness relies on the invariant that after phase k all paths using intermediates {0..k} are known; with k innermost, dist[i][j] is fixed using possibly unimproved dist[i][k] and dist[k][j]. Running the wrong-order loop repeatedly (up to 3 times) does converge, but a single pass is not guaranteed correct.
More Coding Interview Problems questions
- Problem: Build a minimum spanning tree with Kruskal's algorithm and report its total weight. python edges =…
- Problem: A CPU runs tasks (letters); identical tasks need at least n idle-or-other slots between them. Find the minimum total time slots…
- Problem: Children stand in a line with ratings; each gets ≥ 1 candy and a child with a higher rating than a NEIGHBOUR must get more than…
- Problem: Count the ways to make amount 5 from coins [1, 2, 5]. A developer wrote the loops in this order. python def ways(coins, amt): dp…
- Problem: Find the length of the longest palindromic subsequence of s and the minimum number of insertions that make s a palindrome. python…
- Problem: Matrices A1..A6 have dimensions given by d (Ai is d[i−1] × d[i]). Find the minimum number of scalar multiplications to compute…