The brute force is obvious and wrong for an interview.
Start by clarifying the problem constraints and edge cases, then propose an efficient O(n) greedy solution that tracks total gas balance and current tank balance to identify the valid starting station. Explain the key insight that if total gas is less than total cost, no solution exists; otherwise, the starting point is the station after the last point where the tank went negative.
Pro tip: Mention that the greedy approach works because if a car can't reach station B from A, then no station between A and B can be the start, allowing us to skip them. This demonstrates deep understanding and can impress the interviewer.
Ask clarifying questions about input format, constraints, and edge cases (e.g., empty input, negative values). Confirm that gas and cost arrays are of equal length and represent stations in a circle.
Compute the total gas and total cost. If total gas < total cost, return -1 immediately because the trip is impossible regardless of starting point.
Iterate through stations, maintaining a running tank balance and a candidate start index. When the tank balance drops below zero, reset the start to the next station and reset the tank balance to zero.
After the scan, if total gas >= total cost, the candidate start index is the answer; otherwise, return -1. Explain why this works using the circular property.
State that the algorithm runs in O(n) time and O(1) space, which is optimal. Discuss potential variations (e.g., if gas and cost are given as separate arrays).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.