Vending Machine - Problem

Design a comprehensive Vending Machine system that handles different operational states and coin transactions.

Your vending machine should support the following functionality:

  • State Management: Handle three states - 'idle', 'coin_inserted', and 'dispensing'
  • Coin Operations: Accept coins, calculate total inserted amount
  • Product Selection: Allow product selection and verify sufficient funds
  • Change Calculation: Return exact change when overpaid
  • Refunds: Handle refund requests in appropriate states

Implement a VendingMachine class with methods to handle all operations and return appropriate status messages.

The machine should track its current state and reject invalid operations (e.g., selecting product without inserting coins).

Input & Output

Example 1 — Basic Operations
$ Input: operations = [["insert_coin", 25], ["select_product", "coke"], ["get_state"]]
Output: ["Coin inserted. Total: 25. State: coin_inserted", "Dispensing coke. Change: 0", "Current state: idle, Coins: 0"]
💡 Note: Insert 25 cents → state becomes coin_inserted, select coke (costs 25) → dispense with no change and return to idle state
Example 2 — Insufficient Funds
$ Input: operations = [["insert_coin", 10], ["select_product", "coke"], ["refund"]]
Output: ["Coin inserted. Total: 10. State: coin_inserted", "Insufficient funds. Need 15 more", "Refunded: 10. State: idle"]
💡 Note: Insert 10 cents, try to buy coke (25 cents) → insufficient funds, then refund the 10 cents
Example 3 — Invalid Operations
$ Input: operations = [["select_product", "chips"], ["refund"], ["insert_coin", 20]]
Output: ["Please insert coins first", "No coins to refund", "Coin inserted. Total: 20. State: coin_inserted"]
💡 Note: Try to select product without coins → error, try to refund with no coins → error, then insert coins properly

Constraints

  • 1 ≤ operations.length ≤ 100
  • Valid operations: insert_coin, select_product, refund, get_state
  • Coin values: 1 ≤ coin ≤ 100
  • Products: coke (25), chips (15), candy (10)

Visualization

Tap to expand
INPUT OPERATIONS["insert_coin", 25]["select_product", "coke"]["get_state"]Sequential Operations• Commands processed in order• Each affects machine state• Validation required per stateSTATE MACHINEIDLECOIN_INDISPENSE1. Validate Operation2. Execute if Valid3. Update State4. Return MessageRESULTS"Coin inserted. Total: 25.State: coin_inserted""Dispensing coke.Change: 0""Current state: idle,Coins: 0"State Consistency✓ Valid operations succeed✓ Invalid operations rejected✓ State properly maintainedKey Insight:Proper state machine design prevents invalid operations and ensuresconsistent behavior by validating each command against current state.TutorialsPoint - Vending Machine | State Machine Design
Asked in
Amazon 35 Microsoft 28 Google 22 Apple 18
28.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen