Fewest Refuel Stops for a Long Haul Delivery Truck
Last updated: November 29, 2026
Quick Overview
A truck starts with some fuel and must travel a target distance. Fuel stations along the highway are given as (position, fuel available); return the minimum number of stops needed to reach the target, or -1 if it cannot.
Flipkart
November 29, 20260
0
0 solved
A truck starts with some fuel and must travel a target distance. Fuel stations along the highway are given as (position, fuel available); return the minimum number of stops needed to reach the target, or -1 if it cannot.
A greedy plus heap problem used in Flipkart SDE-2 onsites, framed around Ekart trucks running intercity lanes. The interviewer looks for the 'retroactive choice' insight: drive as far as possible and, only when stuck, refuel at the best station already passed.
What the Interviewer Expects
- Explain why DP over stations works but is O(n^2)
- Push fuel of every passed station into a max heap
- When fuel runs out before the next point, pop the largest passed station
- Argue why picking the largest passed fuel is optimal
- State O(n log n) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return which stations to stop at?
- What if each stop has a time cost and fuel has a price?
- How does the solution change if the tank has a maximum capacity?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
targetdistance, startingfuel(1 unit per km), stations sorted by position as(pos, fuel).- Unlimited tank; stopping takes all fuel at that station.
- Output: min number of stops or -1....
High-Level Design
A DP where dp[k] is the farthest reach with k stops works in O(n^2). The greedy is cleaner: drive until fuel runs out. Every station you passed was an option; you did not have to decide at the tim...