Implement a Vending Machine
Company: ZipHQ
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
Design and implement the core logic for a vending machine using object-oriented principles.
The machine stores products, accepts bills, tracks the current customer's balance, dispenses an item after selection, and returns change.
Requirements:
- Each product has an id, a price in cents, and a remaining quantity.
- The machine accepts a fixed set of bill denominations, for example 1, 5, 10, and 20 dollar bills.
- `insertBill(amount)` adds money to the current transaction.
- `selectProduct(productId)` should:
1. verify the product exists and is in stock,
2. verify the customer has inserted enough money,
3. deduct one unit of inventory,
4. compute and dispense change using bills available in the machine,
5. fail gracefully if exact change cannot be made.
- `cancel()` should return the customer's inserted money and reset the transaction.
- The implementation should be testable, and you should write tests for key cases such as insufficient funds, exact payment, overpayment, out-of-stock items, and inability to make exact change.
Assume starter code is provided for the main classes, and implement the missing behavior and tests.
Quick Answer: This question evaluates object-oriented design, stateful transaction management, inventory control, and algorithmic change-making under resource constraints.