Data Structures & AlgorithmsVery hardTCS NQT · Infosys 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,4,4),(4,3,5),(1,0,2),(2,1,5),(3,2,11),(4,3,7)] # (u, v, weight)What distance array (index 0 to 4) does it produce?
- A. 0, 1, 2, 3, 3
- B. 0, 2, 7, 16, 11
- C. 1, 3, 8, 17, 12
- D. 11, 16, 7, 2, 0
Correct answer: B. 0, 2, 7, 16, 11
Repeatedly finalising the closest unvisited vertex and relaxing its edges gives the true shortest-path distances [0, 2, 7, 16, 11] here. Counting edges (hops) instead of summing weights, [0, 1, 2, 3, 3], ignores the actual edge weights and is wrong whenever they aren't all equal.
More Coding Interview Problems questions
- Merging overlapping intervals (sort by start, merge when the next interval's start is ≤ the current merged interval's end — touching…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- 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…