Data Structures & AlgorithmsVery hardGoldman 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 = [(2,5,1),(2,1,7),(2,1,8),(4,2,3),(0,4,2),(3,4,9),(4,0,6),(1,5,7),(2,0,9),(2,0,3)]  # (u, v, weight)
What distance array (index 0 to 5) does it produce?
  1. A. 1, 11, 4, 12, 3, 5
  2. B. 0, 2, 1, 2, 1, 2
  3. C. 4, 2, 11, 3, 10, 0
  4. D. 0, 10, 3, 11, 2, 4

Correct answer: D. 0, 10, 3, 11, 2, 4

Repeatedly finalising the closest unvisited vertex and relaxing its edges gives the true shortest-path distances [0, 10, 3, 11, 2, 4] here. Counting edges (hops) instead of summing weights, [0, 2, 1, 2, 1, 2], ignores the actual edge weights and is wrong whenever they aren't all equal.

More Coding Interview Problems questions

Exams that ask this topic