2Brute2Best
🔥0
P4

Design a Vending Machine

Easy High-frequency interview topic Pro OnlyState
⚡ 30-Second Interview Cheat SheetP4 · 45-Min Design Plan
2 Patterns Combined
1. Scope Boundaries

In a 45-minute LLD interview, the interviewer assesses finite state modeling (eliminating boolean flags and monolithic switch-statements via the State pattern) and transactional integrity (ensuring inventory is never deducted prematurely, and exact refunds/change are calculated using integer currency). Scope the solution to three core lifecycle states (IdleState, HasMoneyState, DispensingState), product slot inventory management, coin/currency accumulation, dispensing with change return, and user-initiated cancellation. Mention cash cassette coin-change depletion (Greedy/DP change maker) and digital UPI/card payments as natural extensions.

2. Core Noun Entities
Product / Slotdata-struct
Inventorystateful-class
VendingStateinterface
VendingMachinestateful-class
3. Hotspot → Pattern
Lifecycle state transitions and permitted operation guards → State (VendingState interface with IdleState, HasMoneyState, DispensingState)
Product inventory and stock deduction → Single Responsibility Principle (Inventory encapsulates slot capacity and stock deduction)
01

Problem statement & clarifying requirements

Design an automated Vending Machine system. The machine must manage inventory across multiple product slots, accept cash/coin insertions, allow item selection, calculate and dispense correct change, and support transaction cancellation with full refunds. The lifecycle must be modeled cleanly using the State pattern so that operations invalid in a given state (such as dispensing without payment or cancelling during dispensing) are rejected without sprawling conditional checks.

Questions you should ask the interviewer first
“What states does the vending machine transition through?”

Three primary lifecycle states: IdleState (waiting for customer), HasMoneyState (customer inserted currency, can select products or cancel), and DispensingState (verifying stock, deducting inventory, releasing product and change).

“What happens if a user selects a product whose price exceeds the inserted balance?”

The machine remains in HasMoneyState, displays the shortfall amount, and preserves the inserted balance so the user can insert more money or press cancel.

“How is change calculated when the inserted balance exceeds the item price?”

The machine calculates change = balance - itemPrice, dispenses the item and change, resets the session balance to 0, and returns to IdleState.

“Can a user cancel their transaction at any time?”

Cancellation is allowed exclusively during HasMoneyState, triggering a full refund of all inserted coins. Cancellation is rejected during IdleState (nothing to refund) and DispensingState (physical delivery in progress).

02

First, sketch the design yourself

Spend 5 minutes listing the classes, who owns whom, and which interface hides the part that changes.

  1. 1.

    Do not use an enum VendingMachineState paired with massive switch (state_) blocks in every method. Use the State pattern to encapsulate each state's rules in dedicated polymorphic classes.

  2. 2.

    Ensure state transitions are explicit and driven by the active state: IdleState transitions to HasMoneyState on coin insertion; HasMoneyState transitions to DispensingState on valid product selection or back to IdleState on refund.

  3. 3.

    Decouple catalog and stock bookkeeping into a standalone Inventory class to honor the Single Responsibility Principle (SRP).

03

How to think through this problem (5-step walkthrough)

Step 1 · Scope the 45-minute target & core boundary

In a 45-minute LLD interview, the interviewer assesses finite state modeling (eliminating boolean flags and monolithic switch-statements via the State pattern) and transactional integrity (ensuring inventory is never deducted prematurely, and exact refunds/change are calculated using integer currency). Scope the solution to three core lifecycle states (IdleState, HasMoneyState, DispensingState), product slot inventory management, coin/currency accumulation, dispensing with change return, and user-initiated cancellation. Mention cash cassette coin-change depletion (Greedy/DP change maker) and digital UPI/card payments as natural extensions.

Step 2 · Extract candidate nouns (and reject false classes)

Not every noun in the prompt deserves its own class. Separate dumb data structs from stateful managers, and turn trivial nouns into fields or enums.

Product / Slot
Value Struct

Product represents an immutable item (name, price in integer rupees/paise), while Slot pairs a Product with its remaining inventory count.

Inventory
Stateful Class / Manager

Encapsulates stock tracking across slot codes (std::unordered_map<std::string, Slot>), providing stock inspection and atomic deduction upon successful purchase.

VendingState
Abstract Interface

Polymorphic State interface declaring permitted operations: insertCoin, selectSlot, dispense, and refundCancel.

VendingMachine
Stateful Class / Manager

Context entity holding the current state (std::unique_ptr<VendingState>), Inventory, accumulated balance, and selected slot, exposing the client-facing API.

Button / DisplayScreen
Reject as Class → Field / Method

Trap noun — beginners often introduce separate Button, Keypad, or DisplayScreen classes. Physical buttons are mere hardware input signals that invoke API methods (selectSlot, refundCancel), and screen text is formatted return output or console logging, not persistent domain entities.

Step 3 · Draft the naive design & spot what breaks

A naive implementation uses an enum State { IDLE, HAS_MONEY, DISPENSING } and writes a switch (state_) block in every method of VendingMachine. Adding a new operation (e.g. requestReceipt or applyPromoCode) forces editing every single switch block. Edge-case bugs proliferate: e.g. a user presses cancel while dispensing is physically in progress, or inserts coins after selecting a sold-out item, corrupting machine balance and inventory.

Step 4 · Map change hotspots to design patterns
Lifecycle state transitions and permitted operation guards→ State (VendingState interface with IdleState, HasMoneyState, DispensingState)

A centralized switch (state_) across all operations violates OCP and turns every new feature into a risk of invalid state combinations (e.g. dispensing without money or cancelling during dispensing).

Product inventory and stock deduction→ Single Responsibility Principle (Inventory encapsulates slot capacity and stock deduction)

Interleaving inventory decrement logic with coin validation leads to double-decrement or lost stock on transaction failure.

Step 5 · Lock down C++23 ownership, invariants & concurrency
  • ✓

    Atomic payment-before-dispense invariant: Inventory is deducted and change is returned only after confirming balance >= product.price and successfully transitioning to DispensingState.

  • ✓

    Refund conservation invariant: In HasMoneyState, cancelling returns the exact accumulated balance to the user and resets the session balance to 0 before transitioning to IdleState.

  • ✓

    Integer currency invariant: All prices, inserted coins, and change amounts are tracked as integers (rupees or paise) to eliminate floating-point rounding errors.

04

Entities & design patterns used

Brute2Best Pro Content

Unlock Full UML Diagram & C++23 Solution

The complete UML diagram, production-ready C++23 code, follow-up extensions, and design pitfall analysis are part of Brute2Best Pro. All 7 foundation chapters are 100% free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Systems Integration

Related C++ Systems & Algorithmic Foundations

Deep dive into underlying layers