Lemonade Change

EasyGreedygreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You run a lemonade stand where each cup costs $5. Customers arrive one at a time in the order given by bills, and each pays with a single $5, $10, or $20 bill. You start with no money at all. Every customer must receive exact change immediately, using only the bills you have collected from earlier customers (and the bill they just handed you is not usable for their own change beyond keeping it). Return true if you can give every customer correct change, and false otherwise.

Examples

Input: bills = [5,5,5,10,20]
Output: true
After three $5s, give one $5 back for the $10, then a $10 and a $5 for the $20.
Input: bills = [5,5,10,10,20]
Output: false
The two $10s use up both $5s, leaving no way to make $15 for the $20.

Constraints

  • 1 <= bills.length <= 10^5
  • Each `bills[i]` is 5, 10, or 20.
  • You start with no bills.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems