Design a vending machine class with inventory, balance, purchases and refunds

Quick Overview

Design a vending machine class that stocks items, accepts money, sells an item by name and refunds the remaining balance. It tests object-oriented design, how you keep state between calls, and returning the right message for each kind of failed purchase.

Design a vending machine class with inventory, balance, purchases and refunds

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a vending machine system that manages inventory, accepts money, and dispenses products. Implement the following methods: - `VendingMachine() -> void`: Initialize an empty vending machine with no items and zero balance. - `addItem(name: string, price: number, quantity: number) -> void`: Add items to inventory. If the item already exists, increase its quantity (price remains unchanged). - `insertMoney(amount: number) -> number`: Insert money into the machine. Returns the new total balance. - `selectItem(name: string) -> string`: Attempt to purchase an item. - If the item doesn't exist, return `"Item not found"`. - If the item is out of stock, return `"Out of stock"`. - If the balance is insufficient, return `"Insufficient funds"`. - Otherwise, dispense the item (decrement its quantity, subtract its price from the balance) and return the item name. - `getBalance() -> number`: Return the current balance. - `refund() -> number`: Return all inserted money and reset the balance to 0. For example, this sequence of calls: ```text Input: ["VendingMachine", "addItem", "addItem", "insertMoney", "selectItem", "getBalance", "selectItem", "refund"] [[], ["Cola", 150, 5], ["Chips", 100, 3], [200], ["Cola"], [], ["Chips"], []] Output: [null, null, null, 200, "Cola", 50, "Insufficient funds", 50] Explanation: vm = VendingMachine() # Initialize vm.addItem("Cola", 150, 5) # Add 5 Colas at price 150 vm.addItem("Chips", 100, 3) # Add 3 Chips at price 100 vm.insertMoney(200) # -> 200 (balance is now 200) vm.selectItem("Cola") # -> "Cola" (purchased, balance = 200 - 150 = 50) vm.getBalance() # -> 50 vm.selectItem("Chips") # -> "Insufficient funds" (need 100, have 50) vm.refund() # -> 50 (returns balance, resets to 0) ``` ```hint What to store Ask which state has to survive between calls, and what `selectItem` needs to look up quickly by name. ``` ### Constraints & Assumptions - `1 <= price <= 10000` - `1 <= quantity <= 1000` - `1 <= amount <= 10000` - `1 <= name.length <= 50` - At most 10,000 total calls will be made. - Prices and amounts are integers. - When more than one failure applies, `selectItem` returns the first one in the order listed above. ### Clarifying Questions to Ask - Are item names case-sensitive, so that `"cola"` and `"Cola"` are different items? - Does the machine need to track which coins or bills were inserted, or only the total amount? - Will calls come from a single thread, or can two purchases run at the same time? ### What a Strong Answer Covers - The state the class keeps between calls and why that structure makes each lookup fast. - `selectItem` checks the failure cases in the required order and changes no state when a purchase fails. - Restocking an existing item keeps its original price, and a sold-out item still reports `"Out of stock"`. - How money is represented, including the refund of a zero balance. - Time and space complexity of each operation. ### Follow-up Questions - How would you pay out change in specific coin denominations, and what should happen when exact change is impossible? - How would you make the class safe if several threads call `selectItem` at once? - When would you model the machine with explicit states (idle, has money, dispensing), and what would that buy you here? - How would you add an operator action that changes a price or removes an item without breaking a customer's purchase in progress?

Overview: Design a vending machine class that stocks items, accepts money, sells an item by name and refunds the remaining balance. It tests object-oriented design, how you keep state between calls, and returning the right message for each kind of failed purchase.

General logo
General
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a vending machine system that manages inventory, accepts money, and dispenses products.

Implement the following methods:

  • VendingMachine() -> void : Initialize an empty vending machine with no items and zero balance.
  • addItem(name: string, price: number, quantity: number) -> void : Add items to inventory. If the item already exists, increase its quantity (price remains unchanged).
  • insertMoney(amount: number) -> number : Insert money into the machine. Returns the new total balance.
  • selectItem(name: string) -> string : Attempt to purchase an item.
    • If the item doesn't exist, return "Item not found" .
    • If the item is out of stock, return "Out of stock" .
    • If the balance is insufficient, return "Insufficient funds" .
    • Otherwise, dispense the item (decrement its quantity, subtract its price from the balance) and return the item name.
  • getBalance() -> number : Return the current balance.
  • refund() -> number : Return all inserted money and reset the balance to 0.

For example, this sequence of calls:

Input:
["VendingMachine", "addItem", "addItem", "insertMoney", "selectItem", "getBalance", "selectItem", "refund"]
[[], ["Cola", 150, 5], ["Chips", 100, 3], [200], ["Cola"], [], ["Chips"], []]
Output:
[null, null, null, 200, "Cola", 50, "Insufficient funds", 50]
Explanation:
vm = VendingMachine()              # Initialize
vm.addItem("Cola", 150, 5)         # Add 5 Colas at price 150
vm.addItem("Chips", 100, 3)        # Add 3 Chips at price 100
vm.insertMoney(200)                # -> 200 (balance is now 200)
vm.selectItem("Cola")              # -> "Cola" (purchased, balance = 200 - 150 = 50)
vm.getBalance()                    # -> 50
vm.selectItem("Chips")             # -> "Insufficient funds" (need 100, have 50)
vm.refund()                        # -> 50 (returns balance, resets to 0)

Constraints & Assumptions

  • 1 <= price <= 10000
  • 1 <= quantity <= 1000
  • 1 <= amount <= 10000
  • 1 <= name.length <= 50
  • At most 10,000 total calls will be made.
  • Prices and amounts are integers.
  • When more than one failure applies, selectItem returns the first one in the order listed above.

Clarifying Questions to Ask Guidance

  • Are item names case-sensitive, so that "cola" and "Cola" are different items?
  • Does the machine need to track which coins or bills were inserted, or only the total amount?
  • Will calls come from a single thread, or can two purchases run at the same time?

What a Strong Answer Covers Guidance

  • The state the class keeps between calls and why that structure makes each lookup fast.
  • selectItem checks the failure cases in the required order and changes no state when a purchase fails.
  • Restocking an existing item keeps its original price, and a sold-out item still reports "Out of stock" .
  • How money is represented, including the refund of a zero balance.
  • Time and space complexity of each operation.

Follow-up Questions Guidance

  • How would you pay out change in specific coin denominations, and what should happen when exact change is impossible?
  • How would you make the class safe if several threads call selectItem at once?
  • When would you model the machine with explicit states (idle, has money, dispensing), and what would that buy you here?
  • How would you add an operator action that changes a price or removes an item without breaking a customer's purchase in progress?