Prepin
Log in

Create a vending machine.

Approach / Explanation

This question is frequently posed by interviewers to assess your knowledge and abilities in system design, software architecture, and object-oriented programming. In this scenario, the interviewer is requesting you to create a design for a vending machine. To effectively respond, you should take into account various elements of system design, including understanding requirements, defining classes and their functionalities, the relationships between classes, methods, class attributes, error and exception management, and how to present your design. Consider the following points: Requirements . You need to grasp what functionalities the vending machine should provide. Typically, a vending machine is capable of selling various products, processing payments, giving change, managing inventory, and displaying inventory information. Classes, functions, and attributes . Determine potential classes (e.g., Product, Inventory, Payment, etc.), their associated methods (e.g., dispenseProduct(), acceptPayment(), etc.), and their attributes (product list, payment amount, change, etc.). Error handling . Recognize potential exceptions or error scenarios and how the system should respond to them.

Suggested Answer

My response will be based on the principles of object-oriented programming. Here are the main elements I am considering: Product: This class would store details about a particular product, including its ID, name, price, and available quantity. Inventory: This class would oversee all the products in the vending machine. It would include methods for adding or removing products, checking product availability, and retrieving product information. PaymentProcessor: This class would manage all payment-related functions. It could process cash or credit/debit card transactions, verify payments, carry out transactions, and return any change as needed. UserInterface: This class would engage with the user, showcasing available options, gathering the user’s selection, presenting payment options, and dispensing the chosen product. VendingMachine: This is the primary class that would incorporate instances of the previously mentioned classes and oversee the overall operation of the vending machine. As for error handling, the VendingMachine class would be responsible for catching any exceptions raised by the other classes and determining the appropriate response.

Alternative Answer

Designing a vending machine can be approached in various ways; however, I will demonstrate my method using a blend of object-oriented and state-machine design. To begin, I would establish several states for the vending machine: Idle, Dispensing, Refunding, and OutOfService. Next, I would create several classes: Product: This class would hold the name and price of each product. Stock: This would oversee the inventory management for each product. MoneyHandler: This class would take care of validating and processing transactions. VendingMachine: This would manage the current state of the system, handle transitions between states, and track the current product selections as well as the amount of money inserted. For error management, any failed operations would lead to a transition to the OutOfService state, and exceptions could be logged, shown, and handled properly. Once the issue is resolved, the machine would ideally revert to the Idle state.

Question Details

Difficulty & Category

Medium
system design

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.