Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA

Problem: Dijkstra is run with a binary heap but WITHOUT the 'skip stale entry' check (if d > dist[u]: continue). What happens?

  1. A. Results stay correct, but a node may be expanded several times, raising the worst-case cost (up to O(E log E) heap work plus repeated scans of its adjacency list)
  2. B. Distances become wrong whenever a node is pushed twice
  3. C. It becomes an infinite loop
  4. D. It is identical in cost and behaviour

Correct answer: A. Results stay correct, but a node may be expanded several times, raising the worst-case cost (up to O(E log E) heap work plus repeated scans of its adjacency list)

A stale entry carries a larger distance; relaxing from it cannot improve any neighbour, so answers stay right. But re-scanning a high-degree node many times wastes time and can blow up on dense graphs. The check keeps each node's expansion to once.

More Coding Interview Problems questions

Exams that ask this topic