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.

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

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
1D dynamic programming
0/1 knapsack ordering
Greedy with heap
Refueling stops
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
  • 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 Problems
Sample 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...


Submit Your Answer
Markdown supported