Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
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 = [(1,0,12),(4,0,4),(3,2,7),(1,2,8),(0,2,5),(2,4,11),(4,3,2),(0,3,7),(0,2,2)] # (u, v, weight)What distance array (index 0 to 4) does it produce?
- A. 4, 6, 2, 10, 0
- B. 0, 1, 1, 1, 1
- C. 1, 11, 3, 7, 5
- D. 0, 10, 2, 6, 4
Correct answer: D. 0, 10, 2, 6, 4
Repeatedly finalising the closest unvisited vertex and relaxing its edges gives the true shortest-path distances [0, 10, 2, 6, 4] here. Counting edges (hops) instead of summing weights, [0, 1, 1, 1, 1], ignores the actual edge weights and is wrong whenever they aren't all equal.
More Coding Interview Problems questions
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- What is the length of the longest common subsequence of "BBBEEBBA" and "BCDDDC"?
- What is the length of the longest common subsequence of "DCDCDAC" and "AABDCD"?
- What is the length of the longest common subsequence of "BAECBABDC" and "EEAEDAB"?
- What is the length of the longest common subsequence of "DCAEECCD" and "BEBEAADD"?
- Merging overlapping intervals (sort by start, merge when the next interval's start is ≤ the current merged interval's end — touching…