Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard
Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest distances from vertex 0 on this undirected weighted graph.
Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest distances from vertex 0 on this undirected weighted graph.
edges = [(3,1,10),(5,3,5),(1,3,12),(0,5,12),(2,1,11),(4,5,1),(1,4,9),(3,0,11),(3,0,12),(4,0,11)] # (u, v, weight)What distance array (index 0 to 5) does it produce?
- A. 1, 21, 32, 12, 12, 13
- B. 12, 11, 11, 31, 20, 0
- C. 0, 20, 31, 11, 11, 12
- D. 0, 2, 3, 1, 1, 1
Correct answer: C. 0, 20, 31, 11, 11, 12
Repeatedly finalising the closest unvisited vertex and relaxing its edges gives the true shortest-path distances [0, 20, 31, 11, 11, 12] here. Counting edges (hops) instead of summing weights, [0, 2, 3, 1, 1, 1], ignores the actual edge weights and is wrong whenever they aren't all equal.
More Coding Interview Problems questions
- Problem: 7 servers are joined by cables one at a time. Count the connected groups at the end and how many cables were redundant (joined…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- What is the length of the longest common subsequence of "ACEACCEBD" and "DBEADDEA"?
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…