Design a hotel vending machine with coin balance, change, restocking and stock queries
Company: Booking.com
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a vending machine for hotels that stocks a fixed set of products. Each product has a name, a price in cents, and an available quantity.
Guests interact with the machine by inserting coins, selecting a product by name, and either completing a purchase or cancelling and recovering their balance. The machine must reject purchases when a product is out of stock or when the inserted amount is insufficient.
The machine also supports administrative actions: restocking an existing product and querying the remaining stock of any product.
Implement the following methods:
- `addProduct(name: str, price: int, quantity: int)`: Add a new product to the machine. If the product already exists, overwrite its price and set its quantity to the given value.
- `restock(name: str, quantity: int) -> bool`: Add the given quantity to an existing product's stock. Return True on success, or False if the product does not exist.
- `insertCoin(coin: int) -> int`: Insert a coin of the given value (cents) into the machine and return the updated balance.
- `selectProduct(name: str) -> str`: Attempt to purchase the named product. On success, decrement stock, deduct the price from the balance, refund the remainder, reset the balance to 0, and return the string `"DISPENSED:<change>"`, where `<change>` is the refunded remainder in cents. If the product does not exist or is out of stock, return `"OUT_OF_STOCK"` (balance unchanged). If the balance is insufficient, return `"INSUFFICIENT_FUNDS"` (balance unchanged).
- `cancel() -> int`: Cancel the current transaction. Return the full balance to the guest and reset the machine's balance to 0.
- `getStock(name: str) -> int`: Return the current stock quantity of the named product, or -1 if the product is not registered with the machine.
Example 1, a sale with change followed by a sold-out product:
```text
Input:
["VendingMachine", "addProduct", "addProduct", "insertCoin", "insertCoin", "selectProduct", "getStock", "selectProduct", "cancel"]
[[], ["water", 150, 2], ["chips", 200, 0], [100], [100], ["water"], ["water"], ["chips"], []]
Output:
[None, None, None, 100, 200, "DISPENSED:50", 1, "OUT_OF_STOCK", 0]
Explanation:
vm = VendingMachine()
vm.addProduct("water", 150, 2) # water costs 150, 2 in stock
vm.addProduct("chips", 200, 0) # chips registered with no stock
vm.insertCoin(100) # -> 100
vm.insertCoin(100) # -> 200
vm.selectProduct("water") # -> "DISPENSED:50" (200 - 150 = 50 returned, balance reset to 0)
vm.getStock("water") # -> 1
vm.selectProduct("chips") # -> "OUT_OF_STOCK" (stock is checked before funds)
vm.cancel() # -> 0 (the sale already returned everything)
```
Example 2, a rejected purchase, a cancel, an overwrite and the administrative calls:
```text
Input:
["VendingMachine", "addProduct", "addProduct", "insertCoin", "selectProduct", "cancel", "restock", "restock", "getStock", "getStock"]
[[], ["soda", 200, 3], ["soda", 250, 1], [100], ["soda"], [], ["soda", 4], ["candy", 3], ["soda"], ["candy"]]
Output:
[None, None, None, 100, "INSUFFICIENT_FUNDS", 100, True, False, 5, -1]
Explanation:
vm = VendingMachine()
vm.addProduct("soda", 200, 3) # soda costs 200, 3 in stock
vm.addProduct("soda", 250, 1) # overwrite: price 250, stock 1
vm.insertCoin(100) # -> 100
vm.selectProduct("soda") # -> "INSUFFICIENT_FUNDS" (100 < 250, balance stays 100)
vm.cancel() # -> 100 (balance reset to 0)
vm.restock("soda", 4) # -> True (stock 1 + 4 = 5)
vm.restock("candy", 3) # -> False (candy was never added)
vm.getStock("soda") # -> 5
vm.getStock("candy") # -> -1
```
```hint Track the balance
Walk through what happens to the balance after a successful sale and after each kind of rejection, and make sure a rejection leaves every field as it was.
```
### Constraints & Assumptions
- `1 <= len(name) <= 30`, and a name consists of lowercase letters and digits
- `1 <= price <= 10000` (cents)
- `0 <= quantity <= 10000`
- `1 <= coin <= 10000`
- At most 1,000 total operations per test case
- The machine starts with no products and a balance of 0.
- When a product is both unavailable and unaffordable, `selectProduct` returns `"OUT_OF_STOCK"`: availability is checked before funds.
### Clarifying Questions to Ask
- Does the machine accept any coin value, or only a fixed set of denominations?
- Is change unlimited, or must the machine pay it from coins it actually holds?
- Can an administrator call `addProduct` or `restock` while a guest has money inserted, and should that change the guest's purchase?
### What a Strong Answer Covers
- The per-product state and the session balance, and how they are stored so each lookup by name is fast.
- The difference between `addProduct` overwriting an entry and `restock` adding to an existing one, including unknown names in `restock` and `getStock`.
- `selectProduct` checking availability before funds, leaving the balance untouched on either rejection, and paying out change with the balance reset on success.
- `cancel` returning the full balance, including 0 right after a completed sale.
- Time and space complexity per operation.
### Follow-up Questions
- How would you pay change from a limited supply of coins, and what should `selectProduct` return when it cannot make exact change?
- How would you add a second payment path, such as charging the purchase to the guest's room, next to coins?
- How would you report an unknown product separately from a sold-out one without breaking existing callers?
- How would you keep the machine consistent if an administrator restocks while a guest's purchase is in progress?
Overview: Design a hotel vending machine class that takes coins, sells a product and pays out change, and lets staff restock products and check remaining stock. It tests object-oriented design, state handling between calls, and keeping the balance correct after every sale and every rejected purchase.