ORIGINAL CODING QUESTION
First feasible refueling start
Each station supplies fuel and has a cost to travel to the next station, with the last wrapping to zero. Start with an empty tank. Return the smallest zero-based start index completing one full loop without a negative tank, or -1.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n fuel values, then n travel costs.
Constraints
1 ≤ n ≤ 200; 0 ≤ fuel,cost ≤ 100.
Example 1
Input
3 1 2 3 2 2 2
Output
1
The smallest feasible start is index 1.
Example 2
Input
2 1 1 2 2
Output
-1
Total fuel is insufficient.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
A negative total fuel balance makes every start impossible.
Hint 2
A failed segment rules out many candidate starts.
Hint 3
If multiple starts are possible, the requested smallest index needs careful tie handling.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →