Design an ATM Machine - Problem
Design an ATM Machine that simulates real ATM operations with banknote management.
Your ATM handles 5 denominations: $20, $50, $100, $200, and $500. The machine starts empty and supports two key operations:
๐ฆ Deposit: Add banknotes to the machine's inventory
๐ฐ Withdraw: Dispense money using a greedy approach - always prioritize larger denominations first
Key Challenge: The withdrawal algorithm is greedy and inflexible. If the greedy approach fails (e.g., trying to use a $500 bill when only $200s would work), the entire transaction is rejected.
Example:
โข Withdraw $300 with inventory: 2ร$50, 1ร$100, 1ร$200
โข โ Success: Uses 1ร$200 + 1ร$100 (greedy works)
โข Withdraw $600 with inventory: 3ร$200, 1ร$500
โข โ Failure: Tries 1ร$500, needs $100 more, but can't break the constraint to use 3ร$200 instead
Your ATM handles 5 denominations: $20, $50, $100, $200, and $500. The machine starts empty and supports two key operations:
๐ฆ Deposit: Add banknotes to the machine's inventory
๐ฐ Withdraw: Dispense money using a greedy approach - always prioritize larger denominations first
Key Challenge: The withdrawal algorithm is greedy and inflexible. If the greedy approach fails (e.g., trying to use a $500 bill when only $200s would work), the entire transaction is rejected.
Example:
โข Withdraw $300 with inventory: 2ร$50, 1ร$100, 1ร$200
โข โ Success: Uses 1ร$200 + 1ร$100 (greedy works)
โข Withdraw $600 with inventory: 3ร$200, 1ร$500
โข โ Failure: Tries 1ร$500, needs $100 more, but can't break the constraint to use 3ร$200 instead
Input & Output
example_1.py โ Basic Operations
$
Input:
atm = ATM()
atm.deposit([0,0,1,2,1]) # 1ร$100, 2ร$200, 1ร$500
result1 = atm.withdraw(600) # Try $600
result2 = atm.withdraw(300) # Try $300
โบ
Output:
[-1]
[0, 0, 1, 1, 0]
๐ก Note:
First withdrawal fails because greedy tries 1ร$500 leaving $100, but can't make $100 with remaining bills (only $200s available). Second withdrawal succeeds: 1ร$200 + 1ร$100 = $300.
example_2.py โ Successful Greedy
$
Input:
atm = ATM()
atm.deposit([0,2,1,1,0]) # 2ร$50, 1ร$100, 1ร$200
result = atm.withdraw(300)
โบ
Output:
[0, 0, 1, 1, 0]
๐ก Note:
Greedy works perfectly: uses 1ร$200 + 1ร$100 = $300. No need to use $50 bills.
example_3.py โ Edge Case Empty
$
Input:
atm = ATM()
result = atm.withdraw(100)
โบ
Output:
[-1]
๐ก Note:
ATM is empty, cannot withdraw anything.
Visualization
Tap to expand
Understanding the Visualization
1
Deposit Phase
Add banknotes to inventory counters
2
Withdrawal Request
Start greedy algorithm from largest denomination
3
Greedy Selection
Use maximum possible of each denomination
4
Validation
Check if exact amount achieved, update or reject
Key Takeaway
๐ฏ Key Insight: The ATM implements a strict greedy strategy that prioritizes larger denominations. This approach is fast (O(1)) but inflexible - it will reject transactions that could succeed with different bill combinations.
Time & Space Complexity
Time Complexity
O(1)
Only 5 denominations, so constant time operations
โ Linear Growth
Space Complexity
O(1)
Only need to store 5 banknote counts
โ Linear Space
Constraints
- 1 โค banknotesCount[i] โค 109
- 1 โค amount โค 109
- At most 5000 calls total to deposit and withdraw
- The ATM starts empty
- Withdrawal must use greedy strategy - no alternative approaches allowed
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code