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?
- A. 1, 11, 4, 12, 3, 5
- B. 0, 2, 1, 2, 1, 2
- C. 4, 2, 11, 3, 10, 0
- 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
- Fractional knapsack, capacity 14 (items can be split). Items (weight, value): (8, 3), (6, 4), (4, 7), (3, 10). Using the greedy…
- 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 "EBBDBAD" and "ACBCBECC"?
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…
- What is the length of the longest common subsequence of "BDCEADE" and "EEABCADA"?
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (2, 10), (6, 14), (4, 13), (2, 13), (8, 15). Using the…