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 = [(3,4,11),(2,1,3),(4,5,12),(1,2,8),(0,3,4),(3,4,3),(4,1,2),(2,1,3)] # (u, v, weight)What distance array (index 0 to 5) does it produce?
- A. 0, 9, 12, 4, 7, 19
- B. 0, 3, 4, 1, 2, 3
- C. 19, 7, 4, 12, 9, 0
- D. 1, 10, 13, 5, 8, 20
Correct answer: A. 0, 9, 12, 4, 7, 19
Repeatedly finalising the closest unvisited vertex and relaxing its edges gives the true shortest-path distances [0, 9, 12, 4, 7, 19] here. Counting edges (hops) instead of summing weights, [0, 3, 4, 1, 2, 3], ignores the actual edge weights and is wrong whenever they aren't all equal.
More Coding Interview Problems questions
- 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…
- Using O(n²) DP (dp[i] = 1 + max(dp[j]) over all j < i with a[j] < a[i]), what is the length of the longest STRICTLY increasing subsequence…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- 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…
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (7, 7), (5, 7), (3, 14), (5, 11), (6, 5). Using the greedy…