PPractick

DSA interview question

Lemonade Change

Easy · Algorithms · Topics: greedy, array, simulation

The problem

A lemonade stand sells each cup for $5. Customers arrive in order and pay with a $5, $10 or $20 bill, and the stand starts with no change. Can it give every customer correct change? Only $5 and $10 bills are ever useful as change. A $10 needs one $5 back. A $20 needs $15 back: either $10 + $5 or $5 + $5 + $5. Greedy prefers $10 + $5, because $5 bills are more versatile (they're the only way to change a $10).

Example: [5, 5, 5, 10, 20] → true.

Function signature

bool lemonadeChange(const vector<int>& bills);

Test cases

InputOutputExplanation
[5, 5, 5, 10, 20]trueThe $20 gets $10 + $5
[5, 5, 10, 10, 20]falseOnly $10 bills left, no $5
[10, 10]falseNo change for the first customer
[5, 10, 5, 20]true
[5, 5, 5, 5, 20, 20]falseThe first $20 uses three $5s, then there's too little left

(Confirm: each cup costs exactly $5; the stand starts with no money; bills are only 5, 10 or 20.)

How interviewers phrase it

Clarifying questions to ask

  1. Does the stand start with no change? (Yes.)
  2. Are the bills only 5, 10 and 20? (Yes.)
  3. Must customers be served in the given order? (Yes.)

Follow-ups to expect

  1. "Why don't you need to track $20 bills?"
  2. "Why prefer $10 + $5 for a $20?"
  3. "Prove greedy never fails when a solution exists."
  4. "What if prices or denominations varied?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in