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

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.

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

Visualization

Tap to expand
ATM Machine: Greedy Withdrawal StrategyInventory Management$500: 1$200: 3$100: 1$50: 2$20: 5Withdrawal Process: $600 Request1Try $500Use: 1ร—$500Left: $1002Try $200Can't use$100 < $2003Try $100Use: 1ร—$100Left: $0 โœ“โœ“SUCCESSDispense:[0,0,1,0,1]Alternative ScenarioWhat if no $100 bills?Greedy: 1ร—$500, need $100Only $200, $50, $20 leftCannot make $100!Result: REJECT [-1]Key InsightATM uses inflexible greedy strategy - largest bills firstIf greedy fails, entire transaction rejected (even if alternatives exist)
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.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
35.0K Views
Medium-High Frequency
~18 min Avg. Time
1.5K 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