Design an ATM Machine - Problem

Design an ATM machine that stores banknotes of 5 denominations: 20, 50, 100, 200, and 500 dollars. Initially the ATM is empty. Users can deposit or withdraw money.

When withdrawing, the machine prioritizes using banknotes of larger values first.

Example: To withdraw $300 with available notes: 2×$50, 1×$100, 1×$200, the machine uses the $100 and $200 notes.

Important: If withdrawal cannot be completed using the greedy approach (largest first), the request is rejected. The machine does NOT try alternative combinations.

Implement the ATM class:

  • ATM() - Initializes the ATM object
  • void deposit(int[] banknotesCount) - Deposits new banknotes in order [$20, $50, $100, $200, $500]
  • int[] withdraw(int amount) - Returns array of length 5 with count of each denomination to withdraw, or [-1] if impossible

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"], params = [[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]]
Output: [[0,0,1,0,1], [-1], [0,1,0,1,1]]
💡 Note: Initialize ATM. Deposit: 1×$100, 2×$200, 1×$500. Withdraw $600: use 1×$100 + 1×$500 = $600. Deposit more notes. Try $600 again: impossible with greedy approach. Withdraw $550: use all remaining notes.
Example 2 — Greedy Limitation
$ Input: operations = ["ATM", "deposit", "withdraw"], params = [[], [[1,1,1,1,1]], [300]]
Output: [[0,0,1,1,0]]
💡 Note: With 1 note of each denomination, withdraw $300: greedy uses 1×$200 + 1×$100 = $300, ignoring the $50 note.
Example 3 — Impossible Withdrawal
$ Input: operations = ["ATM", "deposit", "withdraw"], params = [[], [[0,0,0,2,1]], [150]]
Output: [[-1]]
💡 Note: Available: 2×$200, 1×$500. To withdraw $150, greedy cannot use any notes (all too large), so returns [-1].

Constraints

  • 1 ≤ banknotesCount.length ≤ 5
  • 0 ≤ banknotesCount[i] ≤ 109
  • 1 ≤ amount ≤ 109
  • At most 5000 calls to withdraw and deposit

Visualization

Tap to expand
ATM Machine: Deposit → Withdraw ProcessDEPOSIT[0,0,1,2,1]ATM Storage$20$50$100$200$50000121WITHDRAWAmount: $600Greedy Algorithm ProcessStep 1: $600 ÷ $500 = 1, use 1×$500Remaining: $600 - $500 = $100Step 2: $100 ÷ $200 = 0, skip $200Step 3: $100 ÷ $100 = 1, use 1×$100Result: [0, 0, 1, 0, 1]Success: 1×$100 + 1×$500 = $600🏦 ATM dispenses largest bills first, just like real life!
Understanding the Visualization
1
Initialize
Empty ATM with 5 denomination slots
2
Deposit
Add banknotes to appropriate slots
3
Withdraw
Use largest denominations first, return counts or [-1]
Key Takeaway
🎯 Key Insight: Greedy approach matches real ATM behavior - always use largest denominations first
Asked in
Amazon 35 Microsoft 28 Google 22 Apple 18
28.4K Views
Medium Frequency
~25 min Avg. Time
892 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