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?
- A. 3
- B. 2
- C. 1
- 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
- From [5, 9, 2, 14, 7, 3, 8], keep as many values as possible so that the largest kept value is at most twice the smallest kept value. What…
- How many integers x with 0 <= x <= 255 have a number of 1-bits that is divisible by 3? (Zero has zero 1-bits, which is divisible by 3.)
- Trains arrive at [900, 940, 950, 1100, 1500, 1800] and depart at [910, 1200, 1120, 1130, 1900, 2000] (matched train by train). A platform…
- Problem: Children stand in a line with ratings [1, 0, 2]. Each child gets at least one candy, and a child with a higher rating than a…
- Problem: Water is poured into a container of heights [1, 8, 6, 2, 5, 4, 8, 3, 7]. Choose two lines to maximise the area of water they…
- Problem: Trucks carry boxes: each truck type is (count, units per box), and at most 10 boxes fit on the truck. Maximise units per truck…