DSA interview question
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.
bool lemonadeChange(const vector<int>& bills);
| Input | Output | Explanation |
|---|---|---|
[5, 5, 5, 10, 20] | true | The $20 gets $10 + $5 |
[5, 5, 10, 10, 20] | false | Only $10 bills left, no $5 |
[10, 10] | false | No change for the first customer |
[5, 10, 5, 20] | true | |
[5, 5, 5, 5, 20, 20] | false | The 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.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in