Getting Ready: Vending Machine
Understand the vending machine problem and learn the questions to further simplify this problem.
Problem definition
A vending machine is an automated self-service unit that provides users with products such as snacks, beverages, and chocolates. The machine contains multiple racks with products placed in slots. Users interact with the machine by inserting money, selecting the product they wish to buy, and receiving the selected item if the transaction is valid. The vending machine manages product dispensing based on the selection and payment, handles change, and maintains the state of its inventory.
In this LLD interview case study, your focus will be on:
Managing the inventory of products using racks and slots within a vending machine.
Handling user interactions for inserting money, selecting products, and dispensing items.
Processing payments securely, including calculating and returning change for cash transactions.
Managing different machine states (idle, accepting money, dispensing).
Note: This vending machine design can be adapted to automated retail kiosks, such as ticketing or electronics dispensers.
The diagram below elaborates on the process of product purchasing using the vending machine:
Expectations from the interviewee
Although the vending machine problem is a simpler design problem asked in interviews, the interviewer still has some expectations. The following provides an overview of what the interviewer wants to hear you discuss in more detail during the interview.
States of the vending machine
An interviewer would also expect you to discuss the different states of the vending machine. You may ask the following set of questions:
What function do the vending machines perform? Alternatively, how many different states can the vending machines have?
After inserting money into the machine, what does the system do?
Who presses the vending machine button, and what happens after pressing it?
What does the dispense function do?
If the vending machine is in a dispense state, is it possible to insert money?
If you are in
NoMoneyInsertedState
and try to select a product without paying money, would you be able to select a product?
Money handling
One of the most significant attributes of the vending machine system is how it receives, calculates, and returns money. You may ask the interviewer the questions listed below:
What should the system do if we pay less money than the product price?
What should the system do if we pay more money than the product price?
Can the credit card be used to input money or can only cash be used?
Design approach
We will design this vending machine system using a bottom-up approach:
First, we’ll identify simple core entities like
Product
,Rack
,Slot
, andPayment
, and define their responsibilities.Next, we’ll model inventory and transaction handling, including product management, payment processing, and change return.
Finally, we’ll design the overall
VendingMachine
as a state-driven system, ensuring that our design is modular and extensible by following SOLID principles.
This approach will also consider edge cases, concurrency, and future enhancements such as digital payments or real-time inventory updates. Diagrams and code will be used to illustrate the design in later lessons.
Design pattern
During an interview, it is always a good practice to discuss the design patterns that a vending machine falls under. Stating the design patterns gives the interviewer a positive impression and shows that the interviewee is well-versed in the advanced concepts of object-oriented design.
Try to answer the following question. If you are not familiar with design patterns, don’t worry! You can learn about them by asking questions like, “Define design patterns.”
Let’s explore the requirements of the vending machine problem in the next lesson.