Pick the Starting Charger for a Full Loop of an EV Route
Last updated: November 29, 2026
Quick Overview
An EV drives a circular route of n charging stations; station i provides charge[i] kWh and driving to the next station costs cost[i] kWh. Return the unique starting station from which the car can complete the loop with an initially empty battery, or -1 if impossible.
0
0
0 solved
An EV drives a circular route of n charging stations; station i provides charge[i] kWh and driving to the next station costs cost[i] kWh. Return the unique starting station from which the car can complete the loop with an initially empty battery, or -1 if impossible.
A Google onsite greedy problem framed around Google Maps EV route planning. The interviewer expects the O(n^2) simulation first, then the one-pass greedy, and pays close attention to your proof that skipping past a failure point is safe.
What the Interviewer Expects
- Notes the total charge must be at least the total cost
- Simulates from each start as a baseline
- Derives the one-pass greedy that resets the start after a deficit
- Proves why no station between the old start and the failure point can work
- Handles the n = 1 case
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
- What if the battery has a maximum capacity?
- Return all valid starting stations when the answer is not unique.
- How would you answer this for many different cost arrays as traffic changes?
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: equal-length lists
chargeandcostof non-negative integers. - Output: the starting index that completes the loop, or -1.
- The problem guarantees uniqueness when a solution exists.
- Bat...
High-Level Design
First, if sum(charge) < sum(cost) no start can work. Otherwise scan once, tracking tank, the net energy since the current candidate start. If tank goes negative at station i, the trip from the c...