Fewest Battery Swap Stops for a Delivery Van on a Long Route
Last updated: November 29, 2026
Quick Overview
An electric delivery van starts with some charge and must drive D miles; swap stations along the route each add a fixed number of miles of range. Return the fewest stops needed to finish, or -1 if impossible.
0
0
0 solved
An electric delivery van starts with some charge and must drive D miles; swap stations along the route each add a fixed number of miles of range. Return the fewest stops needed to finish, or -1 if impossible.
A Google onsite hard with two strong answers: a 1D DP over the number of stops and a greedy with a max-heap. Interviewers like to see the DP stated precisely and then an argument for why the heap greedy is also optimal.
What the Interviewer Expects
- Define reach[t] as the farthest distance achievable with exactly t stops
- Update reach in descending t for each station, 0/1 knapsack style
- Return the smallest t with reach[t] >= D
- Present the heap greedy as the faster alternative
- Explain why stopping where you passed is fine in hindsight
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
- Each station charges a price per mile; minimize cost instead.
- The battery has a maximum capacity.
- Return which stations to stop at.
- Stations appear online as the van drives.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: target distance D, starting range R, and list stations of (position, miles) sorted by position. Stopping at a station adds its miles to the remaining range (no capacity limit). Output the min...
High-Level Design
Let reach[t] be the farthest position reachable using exactly t stops among stations processed so far, with reach[0] = R. For station i at position p with m miles, for t from i down to 0: if `reac...