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?
- 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)
- B. Distances become wrong whenever a node is pushed twice
- C. It becomes an infinite loop
- 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
- Problem: n ≤ 10⁵ points on a line; answer 'how many points lie in [l, r]?' for 10⁵ queries. What is the simplest efficient solution?
- Problem: Implement a queue using two stacks and report the dequeued values. python class Q: def init(self): self.inb, self.out = [], []…
- Problem: Evaluate a basic calculator expression with +, −, parentheses and spaces. python def calc(s): res, num, sign, st = 0, 0, 1, []…
- Problem: Decode a string like 3[a2[c]] where k[...] repeats the bracket content k times. python def decode(s): st, cur, k = [], '', 0 for…
- Problem: For each element, find the next greater element in a circular array (−1 if none). python def ngecircular(a): n = len(a); res =…
- Problem: Compute the stock span: for each day, the number of consecutive days up to today with price ≤ today's price. python def spans(p)…