Design a vending machine
MediumConceptClassic LLD Problems
Answer
Model a machine that accepts coins or notes, lets the user select a product, dispenses it with change, and handles cancellation and out-of-stock items. Its behaviour depends on its current state, so the State pattern fits naturally.
- Core classes:
VendingMachine(the context),Inventorymapping slot codes to aProductand quantity,CoinandNoteenums, and aStateinterface. - States:
Idle,HasMoney,DispensingandOutOfService; each implementsinsertMoney(),selectProduct(),dispense()andcancel(), rejecting actions that are invalid in that state. - Change: compute change from the denominations currently held, and refund the payment if exact change cannot be made.
- Extensions: card or UPI payments through a
PaymentStrategy, and an admin interface for restocking.
More in Classic LLD Problems
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | Design a parking lot system | Concept | Medium | Read the answer to Design a parking lot system | |||
| 2 | Design a vending machine | Concept | Medium | Read the answer to Design a vending machine | |||
| 3 | Design an elevator system | Concept | Hard | Read the answer to Design an elevator system | |||
| 4 | Design a movie ticket booking system | Concept | Hard | Read the answer to Design a movie ticket booking system |