Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: Which statement about Dijkstra's algorithm and negative edge weights is correct?
- 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
- B. Negative weights only affect the algorithm's time complexity, not correctness
- C. Dijkstra's algorithm handles negative weights correctly as long as there's no negative cycle
- 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
- Problem: Which best describes the relationship between DFS-based cycle detection in a directed graph and the 'white-gray-black' (or…
- Problem: A bipartite graph check is commonly done by attempting a 2-coloring via BFS/DFS. What does finding an edge between two…
- Problem: What is the time complexity of finding the lowest common ancestor (LCA) of two nodes in a binary tree with n nodes, using a…
- Problem: With O(n) preprocessing (Euler tour + sparse table for range-minimum query), LCA queries on a tree of n nodes can be answered in…
- Problem: Which statement correctly describes the diameter of a tree (the longest path between any two nodes) and how to compute it…
- Problem: A min-heap (binary heap) supports insert and extract-min in what time complexity, and what is the time to build a heap from n…