Design a vending machine
Easy30 minFree, no account
A state machine with money in it, which is the version that has to be right.
The question
Design a vending machine: select a product, insert coins, dispense, give change.
Handle cancellation at any point, and the case where the machine cannot make change.
Functional
- Select a product, insert coins incrementally, dispense when paid.
- Refund everything on cancel.
- Give correct change, or refuse the sale before taking the money.
- Restock and collect cash as an operator.
Non-functional
- Money must never be created or destroyed by any sequence of actions.
- A power cut mid-sale must not lose the customer's coins.
30:00Commit to an answer before you open the solution. Reading it first teaches you to recognise good answers, which is not the skill being tested.
Stuck?
0 of 3 hints takenThe worked solution
written by a person · not a gradeScore yourself
0 of 5 marked- A state machine written before the classes, with illegal transitions named30
- Stated the money invariant and used escrow to preserve it30
- Change computed correctly, with greedy's limits acknowledged20
- Change checked before taking payment, not after15
- Addressed persistence across a power cut5
We run no AI here and nothing on this page grades you. The score is yours, and the useful number is the one you get on the same problem a month from now, cold.
kept in this browser only