Data Structures & AlgorithmsHardGoogle · Flipkart interview
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 = [(0,2,3),(0,4,11),(0,3,8),(4,3,8),(2,3,4),(3,1,12)] # (u, v, weight)What distance array (index 0 to 4) does it produce?
- A. 11, 7, 3, 19, 0
- B. 1, 20, 4, 8, 12
- C. 0, 19, 3, 7, 11
- D. 0, 2, 1, 1, 1
Correct answer: C. 0, 19, 3, 7, 11
Repeatedly finalising the closest unvisited vertex and relaxing its edges gives the true shortest-path distances [0, 19, 3, 7, 11] here. Counting edges (hops) instead of summing weights, [0, 2, 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…
- 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…
- What is the length of the longest common subsequence of "BEADCBBDA" and "BAAEDC"?
- A divide-and-conquer routine satisfies T(n) = 8T(n/2) + n². What is its running time (by the master theorem, extended for polylog terms…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…