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.

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

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
Greedy
Prefix Sums
Circular Arrays
Proof of Correctness
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
  • 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 Problems
Sample Answer
Requirements
  • Input: equal-length lists charge and cost of 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...


Submit Your Answer
Markdown supported