Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: Which statement about Dijkstra's algorithm and negative edge weights is correct?

  1. A. Dijkstra's algorithm can produce incorrect shortest paths if any edge weight is negative, because it greedily finalizes a node's distance assuming no cheaper path can appear later
  2. B. Negative weights only affect the algorithm's time complexity, not correctness
  3. C. Dijkstra's algorithm handles negative weights correctly as long as there's no negative cycle
  4. D. Dijkstra's algorithm is the standard choice whenever negative weights are present

Correct answer: A. Dijkstra's algorithm can produce incorrect shortest paths if any edge weight is negative, because it greedily finalizes a node's distance assuming no cheaper path can appear later

Dijkstra finalizes (never revisits) the closest unvisited node's distance; a later negative edge could make a previously-dismissed path cheaper, which the algorithm can no longer discover, making its greedy assumption unsound with negative edges.

More Coding Interview Problems questions

Exams that ask this topic