Fewest Refueling Stops for a Long-Haul Amazon Freight Truck

Last updated: November 29, 2026

Quick Overview

A truck must travel target miles, starting with startFuel. Fuel stations at sorted positions offer given amounts of fuel, one mile per unit. Return the minimum number of stops to reach the target, or -1 if impossible.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A truck must travel target miles, starting with startFuel. Fuel stations at sorted positions offer given amounts of fuel, one mile per unit. Return the minimum number of stops to reach the target, or -1 if impossible.

A Hard greedy heap problem that maps directly to Amazon middle-mile freight planning. Interviewers like it because the elegant solution defers decisions: you drive as far as you can and only retroactively choose the best station you passed.

What the Interviewer Expects
  • Recognize the retroactive greedy: pass stations, remember their fuel in a max-heap
  • When fuel runs out before the next point, take the largest remembered fuel
  • Treat the target as a final checkpoint
  • Return -1 when the heap is empty and fuel is short
  • Mention the O(n^2) DP alternative and why greedy is better
Key Topics to Cover
heaps
greedy
dynamic programming
route planning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would the DP over 'farthest reach with i stops' work?
  • What if the truck had a tank capacity limit?
  • How would you minimize cost if stations charged different prices?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Inputs: target > 0, startFuel >= 0, list of [position, fuel] sorted by position with 0 < position < target. Output: minimum stops or -1....

High-Level Design

Keep reach, the farthest point reachable with stops chosen so far. Iterate over checkpoints (each station, then the target). While reach is less than the checkpoint position, pop the largest fuel fr...


Submit Your Answer
Markdown supported