Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA
Problem: The gas-station problem: gas = [1, 2, 3, 4, 5] and cost = [3, 4, 5, 1, 2] around a circular route of 5 stations, starting with an empty tank. What is the index (0-based) of the starting station from which you can complete the circuit?
- A. 4
- B. 2
- C. 5
- D. 3
Correct answer: D. 3
If total gas is at least total cost, a valid start exists; when the running tank drops below zero, the next station is the only candidate worth checking. Starting at index 0 fails here.
More Coding Interview Problems questions
- Problem: Children with greed factors g = [1, 2, 3] and cookies s = [1, 1] (each child takes at most one cookie, and a cookie of size ≥ the…
- Problem: Balloons are shot with arrows that burst every balloon whose horizontal interval contains the arrow's x position. Intervals are…
- Problem: A stream of integers arrives: 4, 9, 1, 7, 8, 3, 6. After each arrival from the 2nd onwards, we report the 2nd largest value seen…
- Problem: How many binary search trees can be formed with exactly 5 distinct keys?
- Problem: The quadratic residues modulo 8: how many integers x in 1 ≤ x ≤ 100 satisfy x² ≡ 1 (mod 8)?
- Problem: Dijkstra's shortest distances from source 0 in the directed graph 0→1 (3), 0→2 (1), 2→1 (1), 1→3 (2), 2→3 (6), 3→4 (1), 1→4 (9)…