Data Structures & AlgorithmsVery hardProduct company OA
Flights (directed, cost): 0→1 100, 1→2 100, 2→3 100, 0→2 500, 1→3 600, 0→3 900. What is the cheapest cost from 0 to 3 using AT MOST 1 stopover (at most 2 flights)? (Run Bellman–Ford for exactly k + 1 = 2 rounds, relaxing from a copy of the previous round.)
- A. 600
- B. 300
- C. 700
- D. 900
Correct answer: A. 600
Two-flight routes to 3: 0→1→3 = 700, 0→2→3 = 600; direct 0→3 = 900. Best within the limit: 600. The overall cheapest 0→1→2→3 = 300 needs 2 stopovers. Relaxing in place (without the copy) can chain several edges in one round and wrongly report 300.
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
- What is the diameter (largest distance between any two nodes) of the weighted tree with edges 1–2 (3), 1–3 (4), 2–4 (2), 2–5 (6), 3–6 (5)…
- Running median with two heaps (a max-heap for the lower half, a min-heap for the upper half, sizes differing by at most 1, the lower half…
- Each array is a binary tree in level order (0-indexed, children at 2i + 1 and 2i + 2, all positions present). Which one is a valid BST?
- Binary trees with node labels A, B, C have preorder A B C and postorder C B A. How many different binary trees match BOTH traversals?
- An alien language uses only the letters a, b, c, d. The words bca, bcd, ad, ac, dc are listed in sorted order. Building an edge from each…
- What is the maximum flow from S to T in the network with capacities S→A 10, S→B 5, A→B 15, A→T 6, B→T 10?