Data Structures & AlgorithmsHardInfosys SP/DSE · Product Company SDE
Problem: A network has V = 10⁵ routers and E = 2 × 10⁵ links with latencies that can be negative (but no negative cycle). Which shortest-path algorithm is correct?
- A. Dijkstra with a binary heap – it handles negative edges
- B. Plain BFS – it ignores weights
- C. Prim's algorithm – it builds shortest paths
- D. Bellman–Ford (O(VE)) or SPFA – Dijkstra may finalise a node too early when an edge is negative
Correct answer: D. Bellman–Ford (O(VE)) or SPFA – Dijkstra may finalise a node too early when an edge is negative
Dijkstra assumes a popped distance can never improve, which a negative edge can break. Bellman–Ford relaxes all edges V − 1 times and is correct with negative edges (here O(VE) = 2 × 10¹⁰ is too slow, so in practice one uses SPFA or reweighting as in Johnson's algorithm, but Dijkstra alone is wrong). Prim builds a minimum spanning tree, not shortest paths.
More Coding Interview Problems questions
- Problem: Union-find with path compression answers which friends are in the same circle. How many circles remain after the unions? python…
- Problem: Find the minimum total cost to connect all offices (minimum spanning tree) using Kruskal's algorithm. python def kruskal(n…
- Problem: Find the length of the shortest transformation from a start word to an end word, changing one letter at a time, every…
- Problem: Determine whether a graph can be coloured with two colours so that no edge joins equal colours (bipartite). python from…
- Problem: Count the number of shortest paths (fewest edges) from node 0 to node n − 1 in an unweighted graph. python from collections…
- Problem: In a grid, 0 is open and 1 is a wall. Find the shortest path length (in cells) from the top-left to the bottom-right moving in 8…