Lemonade Change

easy

At your lemonade stand each glass costs $5. Customers queue up and pay with a $5, $10, or $20 bill; you must give each customer correct change so that their net payment is exactly $5. You start with no change.

Given the bills in the order customers pay, return true if you can give every customer correct change, and false otherwise.

Hints

Which denominations do you ever actually hand back as change?
A $20 needs $15 back — in one of two ways. Which should you prefer?
Prefer $10 + $5 over three $5s, because $5s are the only change for a $10.

Common doubts

Because $5s are the only bill that makes change for a $10. Spending three of them when a $10 + $5 would do can leave a later $10 customer stranded.
No — a $20 is never used as change, so it's irrelevant once collected. Only $5 and $10 counts matter.
Yes. The moment you can't make a customer's change, no future customer can fix it, so you can return false immediately.

Interview follow-ups

General change-making is no longer safely greedy — for arbitrary coin systems you may need dynamic programming to decide feasibility.
Return the index instead of a boolean: fail at the first bill where the greedy change can't be made.

Fun facts

  • This is the canonical example of why greedy change works for some denomination systems (like 5/10/20) but not all.
  • The 'spend the least reusable resource first' heuristic reappears in cache eviction and register allocation.

Asked at

AmazonAdobe
Frequently Sometimes Occasionally
Example 1
Input: bills = [5,5,5,10,20]
Output: true
Collect three $5s, give a $5 change for the $10, then $10+$5 for the $20. Everyone gets change.
Example 2
Input: bills = [5,5,10,10,20]
Output: false
By the $20 customer you hold no $5s (two $10s), so you cannot make $15.
Constraints

- 1 <= bills.length <= 10^5 - bills[i] is either 5, 10, or 20.

Solve this problem →