Skip to content
We areTOP 1%

Design a vending machine

MediumConceptClassic LLD Problems
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

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), Inventory mapping slot codes to a Product and quantity, Coin and Note enums, and a State interface.
  • States: Idle, HasMoney, Dispensing and OutOfService; each implements insertMoney(), selectProduct(), dispense() and cancel(), 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

Classic LLD Problems questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Design a parking lot system
MediumConcept
ConceptMedium Read the answer to Design a parking lot system
2Design a vending machine
MediumConcept
ConceptMedium Read the answer to Design a vending machine
3Design an elevator system
HardConcept
ConceptHard Read the answer to Design an elevator system
4Design a movie ticket booking system
HardConcept
ConceptHard Read the answer to Design a movie ticket booking system

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account