โ† Amazon questions

Minimum cost to reach the last station with chocolates

Filed under Amazon

Free read 3 of 3

Understand movement mechanics: moving from i to i+1 changes our inventory by A[i] - A[i+1].

The question

Given an array A representing chocolates at each station and a cost P to buy a chocolate, find the minimum cost to reach the last station such that the chocolate count never drops below zero at any step.

Constraints

  • 1 <= A.length <= 10^5
  • 0 <= A[i] <= 1000
  • Cost P >= 0

Examples

Input
A = [3, 2, 5], P = 2
Output
2
Why
At station 0 we have 3, moving to station 1 loses 1 chocolate. Buying 1 chocolate at station 1 costs 2 to ensure we never drop below zero.

Approach

  1. Understand movement mechanics: moving from i to i+1 changes our inventory by A[i] - A[i+1].

Tips

  • What they are testing: Test cases where initial chocolates are sufficient vs. cases requiring continuous purchases.
  • Common mistake: Failing to maintain a running balance and greedily buying only at the exact point where balance drops below zero.

Related Amazon questions

Collected from public candidate posts and coding forums. Updated Nov 2026.