Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern
Directed edges: A→B (6), A→C (6), C→B (-2), B→D (3). Dijkstra's algorithm, which finalises each vertex once when it is removed from the priority queue, is run from A. What distance does it report for D, and what is the true shortest distance?
- A. Reports 7; true distance 7
- B. Reports 9; true distance 9
- C. Reports 7; true distance 9
- D. Reports 9; true distance 7
Correct answer: D. Reports 9; true distance 7
Dijkstra finalises B before exploring the negative edge C→B, so it never lowers B's distance again even though C→B would give a shorter path; this gives a reported distance of 9 for D, while the true shortest distance (allowing the negative edge to be used) is 7. Dijkstra does not work correctly with negative edge weights.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Keys 8, 91, 32, 54, 14, 31, 17, 21, 26 are inserted via separate chaining (h(k) = k mod 5, new keys appended to the end of the chain) into…
- Keys 56, 62, 60, 92, 46, 65, 74, 38, 72 are inserted in this order into an empty BST (no rebalancing). What is the height of the tree…
- A binary tree has preorder traversal [7, 75, 8, 32, 9, 18, 91, 86] and inorder traversal [7, 8, 9, 18, 32, 75, 86, 91]. Reconstructing the…
- Keys 43, 53, 63 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- A disjoint-set (union-find with union by rank and path compression) over 8 elements (0 to 7), each initially its own component, runs this…
- Undirected graph edges: D–C, B–C, A–C, A–B, A–D, B–D. Using a BFS 2-colouring, is this graph bipartite?