Data Structures & AlgorithmsHardIBM · Deloitte pattern
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,1,4),(3,4,4),(4,5,7),(0,2,7),(5,4,2),(4,3,12),(0,1,2),(5,0,11)] # (u, v, weight)What distance array (index 0 to 5) does it produce?
- A. 0, 1, 1, 3, 2, 1
- B. 1, 3, 7, 18, 14, 12
- C. 0, 2, 6, 17, 13, 11
- D. 11, 13, 17, 6, 2, 0
Correct answer: C. 0, 2, 6, 17, 13, 11
Repeatedly finalising the closest unvisited vertex and relaxing its edges gives the true shortest-path distances [0, 2, 6, 17, 13, 11] here. Counting edges (hops) instead of summing weights, [0, 1, 1, 3, 2, 1], ignores the actual edge weights and is wrong whenever they aren't all equal.
More Coding Interview Problems questions
- What is the length of the longest common subsequence of "ADDDBEEE" and "DDCCCCDB"?
- 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…
- Problem: 8 servers are joined by cables one at a time. Count the connected groups at the end and how many cables were redundant (joined…
- Merging overlapping intervals (sort by start, merge when the next interval's start is ≤ the current merged interval's end — touching…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Problem: 7 servers are joined by cables one at a time. Count the connected groups at the end and how many cables were redundant (joined…