Data Structures & AlgorithmsVery hardGoogle · 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 = [(4,3,6),(3,0,4),(2,5,10),(3,1,8),(1,2,6),(5,4,9),(1,5,11),(4,3,1),(5,2,5)] # (u, v, weight)What distance array (index 0 to 5) does it produce?
- A. 1, 13, 19, 5, 6, 15
- B. 14, 5, 4, 18, 12, 0
- C. 0, 2, 3, 1, 2, 3
- D. 0, 12, 18, 4, 5, 14
Correct answer: D. 0, 12, 18, 4, 5, 14
Repeatedly finalising the closest unvisited vertex and relaxing its edges gives the true shortest-path distances [0, 12, 18, 4, 5, 14] here. Counting edges (hops) instead of summing weights, [0, 2, 3, 1, 2, 3], ignores the actual edge weights and is wrong whenever they aren't all equal.
More Coding Interview Problems questions
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- 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 11 (items can be split). Items (weight, value): (5, 15), (4, 10), (4, 14), (4, 7). Using the greedy…
- A divide-and-conquer routine satisfies T(n) = 2T(n/2) + n / log n. What is its running time (by the master theorem, extended for polylog…
- Fractional knapsack, capacity 12 (items can be split). Items (weight, value): (5, 7), (6, 11), (7, 7), (4, 15). Using the greedy…