Design a Vending Machine
| Difficulty: Easy | Source: awesome-low-level-design |
Requirements
- Multiple products with different prices and quantities
- Accept coins and notes of different denominations
- Dispense product and return change
- Track available products and quantities
- Handle multiple transactions concurrently
- Interface for restocking and collecting money
- Handle insufficient funds or out-of-stock
Class Diagram

Classes, Interfaces and Enumerations
| Class/Interface | Description |
|---|---|
| Product | name, price |
| Coin | Enum for coin denominations |
| Note | Enum for note denominations |
| Inventory | Manages products/quantities; uses concurrent hash map |
| VendingMachineState | Interface for different states (idle, ready, dispense) |
| IdleState | Implements state interface; waiting for user interaction |
| ReadyState | Implements state interface; product selected, awaiting payment |
| DispenseState | Implements state interface; dispensing product and returning change |
| VendingMachine | Singleton; manages state, product, payment |
Design Patterns Used
| Pattern | Application |
|---|---|
| State | VendingMachine transitions between IdleState, ReadyState, and DispenseState |
| Singleton | VendingMachine has a single instance |
Code Implementations
| Language | Source Code |
|---|---|
| Java | View on GitHub |
| Python | View on GitHub |
| C++ | View on GitHub |
| C# | View on GitHub |
| Go | View on GitHub |