Question presented to candidate:
"There are n petrol stations on a circular road. Station i has gas[i] units of fuel, and driving from station i to the next one costs cost[i]. Your car starts empty with an unlimited tank. Write a Python function that returns the index of the station to start from to drive around the circle once, or -1 if that is impossible."
What a strong answer should cover:
- Reduce each station to its net gain
gas[i] - cost[i]. - Fact 1: if the total gain is negative, no start works; otherwise one does.
- Fact 2: if the tank goes negative after station j, no station from the current start to j can be a start, so jump to j + 1 and reset the tank.
- The one-pass solution with
total,tankandstart: O(n) time, O(1) space โ and the prefix-sum argument for why the final start works. - The O(nยฒ) brute force as a baseline, and the
% nwrap-around it needs. - Python details:
zip(gas, cost)orrange(len(gas)), integers that cannot overflow, and that the answer is a station index (not a fuel amount).
Clarifying questions expected:
- Does the tank start empty, with no capacity limit?
- Is the answer guaranteed to be unique, and what should be returned when there is none?
Code / implementation expected: Yes โ can_complete_circuit(gas, cost) in Python 3, one pass.