Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA

A car starts at position 0 with 10 litres of fuel. It must reach position 40; it uses one litre per unit of distance. Gas stations at positions (distance, litres) are (10,15), (20,20), (25,15), (30,25). Stopping at a station adds its litres. What is the minimum number of stops needed?

  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4

Correct answer: B. 2

Greedy with a max-heap: drive as far as fuel allows, and whenever you run short, add the largest fuel from stations already passed. Refuelling at the first station you pass instead of the largest one gives extra stops.

More Coding Interview Problems questions

Exams that ask this topic