Skip to main content

Design an ATM Machine - Solution & Explanation

MediumArrayGreedyDesign18 min readAsked at: Google, Yandex
Practice this problem

Problem Statement

There is an ATM machine that stores banknotes of 5 denominations: 20, 50, 100, 200, and 500 dollars. Initially the ATM is empty. The user can use the machine to deposit or withdraw any amount of money.

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

  • For example, if you want to withdraw $300 and there are 2 $50 banknotes, 1 $100 banknote, and 1 $200 banknote, then the machine will use the $100 and $200 banknotes.
  • However, if you try to withdraw $600 and there are 3 $200 banknotes and 1 $500 banknote, then the withdraw request will be rejected because the machine will first try to use the $500 banknote and then be unable to use banknotes to complete the remaining $100. Note that the machine is not allowed to use the $200 banknotes instead of the $500 banknote.

Implement the ATM class:

  • ATM() Initializes the ATM object.
  • void deposit(int[] banknotesCount) Deposits new banknotes in the order $20, $50, $100, $200, and $500.
  • int[] withdraw(int amount) Returns an array of length 5 of the number of banknotes that will be handed to the user in the order $20, $50, $100, $200, and $500, and update the number of banknotes in the ATM after withdrawing. Returns [-1] if it is not possible (do not withdraw any banknotes in this case).

 

Example 1:

Input
["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"]
[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]]
Output
[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]]

Explanation
ATM atm = new ATM();
atm.deposit([0,0,1,2,1]); // Deposits 1 $100 banknote, 2 $200 banknotes,
                          // and 1 $500 banknote.
atm.withdraw(600);        // Returns [0,0,1,0,1]. The machine uses 1 $100 banknote
                          // and 1 $500 banknote. The banknotes left over in the
                          // machine are [0,0,0,2,0].
atm.deposit([0,1,0,1,1]); // Deposits 1 $50, $200, and $500 banknote.
                          // The banknotes in the machine are now [0,1,0,3,1].
atm.withdraw(600);        // Returns [-1]. The machine will try to use a $500 banknote
                          // and then be unable to complete the remaining $100,
                          // so the withdraw request will be rejected.
                          // Since the request is rejected, the number of banknotes
                          // in the machine is not modified.
atm.withdraw(550);        // Returns [0,1,0,0,1]. The machine uses 1 $50 banknote
                          // and 1 $500 banknote.

 

Constraints:

  • banknotesCount.length == 5
  • 0 <= banknotesCount[i] <= 109
  • 1 <= amount <= 109
  • At most 5000 calls in total will be made to withdraw and deposit.
  • At least one call will be made to each function withdraw and deposit.
  • Sum of banknotesCount[i] in all deposits doesn't exceed 109

Approach Overview

Problem Overview: Design a class that simulates an ATM. The ATM stores banknotes of fixed denominations [20, 50, 100, 200, 500]. You must support two operations: deposit to add notes and withdraw to return a valid combination of notes that equals a requested amount while prioritizing larger denominations.

Approach 1: Greedy Approach Using Arrays (O(1) time, O(1) space)

The ATM only supports five denominations, so the simplest structure is an array that tracks how many notes of each type are available. During deposit, iterate through the array and add the incoming counts directly. For withdraw, iterate from the largest denomination (500) down to the smallest (20) and greedily take as many notes as possible without exceeding the remaining amount. This works because the denominations form a canonical currency system where choosing larger notes first does not block valid solutions. If the remaining amount becomes zero, update the stored counts; otherwise return -1. Each operation processes a fixed set of five values, so both time and space complexity remain O(1). This approach relies on simple array indexing and a classic greedy selection strategy.

Approach 2: Hash Map Approach (O(1) time, O(1) space)

A hash map can also store the number of banknotes per denomination, mapping values like {20: count, 50: count, ...}. Deposits update counts through direct key lookups. Withdrawals still follow the same greedy logic: iterate through denominations in descending order and compute how many notes can be used using min(available, remaining / value). While this design is slightly more flexible if denominations change dynamically, it adds hashing overhead and is less cache-friendly than a fixed array. Complexity remains O(1) time and O(1) space because the number of denominations is constant. The approach fits well when modeling a configurable system in a design style problem.

Recommended for interviews: The array-based greedy implementation is the expected solution. Interviewers want to see that you recognize the fixed denomination set and store counts efficiently. Start with the greedy withdrawal logic from the largest denomination and carefully handle the rollback case when the exact amount cannot be formed. The hash map version demonstrates design flexibility, but the array solution is simpler and closer to production-quality code for a constrained system.

Approach 1: Greedy Approach Using Arrays

This approach uses a greedy algorithm that starts by attempting to fulfill the withdrawal request using the largest denominations available. The solution maintains an array to hold the count of each denomination in the ATM. During withdrawal, it tries to use the largest denominations first and updates the array accordingly.

This C solution makes use of a structure for ATM which maintains the banknotes array representing the count of each denomination. The 'deposit' function adds the respective counts to the banknotes array, while the 'withdraw' function tries to fulfill a withdrawal request starting from the largest denomination, updating the array appropriately. If a withdrawal cannot be fulfilled, it returns an array with -1.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) for both deposit and withdraw since we are iterating over a constant number of denominations.
Space Complexity: O(1) since we are only storing the state of banknotes in the ATM.

Try this approach in the editor →

Approach 2: Hash Map Approach

In this approach, we can use a dictionary (or hash map) structure instead of arrays to store and access the banknotes. This allows for more flexibility if additional denominations are introduced in the future.

This is a Python solution using dictionary to store the counts of banknotes, which can easily handle changes in denominations. The deposit method adds numbers to corresponding denominations, and the withdraw method starts using the highest denominations while adjusting the withdrawal amount. If it can fully meet the withdrawal, it deducts used notes and returns the count of each denomination, otherwise returns [-1] if it's not possible.

Code

Python

JavaScript

Complexity

Time Complexity: O(1) for both operations since they work over a constant set of keys.
Space Complexity: O(1) due to the fixed storage needs.

Try this approach in the editor →

Approach 3: Simulation

We use an array d to record the denominations of the bills and an array cnt to record the number of bills for each denomination.

For the deposit operation, we simply add the number of bills to the corresponding denomination. The time complexity is O(1).

For the withdraw operation, we enumerate the bills from largest to smallest denomination, taking out as many bills as possible without exceeding amount. We then subtract the total value of the withdrawn bills from amount. If amount is still greater than 0 at the end, it means we cannot withdraw the requested amount, and we return -1. Otherwise, we return the number of withdrawn bills. The time complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Greedy Approach Using Arrays

Time Complexity: O(1) for both deposit and withdraw since we are iterating over a constant number of denominations.
Space Complexity: O(1) since we are only storing the state of banknotes in the ATM.

Hash Map Approach

Time Complexity: O(1) for both operations since they work over a constant set of keys.
Space Complexity: O(1) due to the fixed storage needs.

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Greedy Approach Using ArraysO(1)O(1)Best choice when denominations are fixed and known in advance
Hash Map ApproachO(1)O(1)Useful when denominations might change or need dynamic configuration

Video Solution

2241. Design an ATM Machine (Leetcode Medium) • Programming Live with Larry • 4,518 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Design an ATM Machine easy or hard?
The problem is rated Medium because it combines greedy reasoning with class design. The logic itself is straightforward, but you must carefully manage note counts and handle the case where a withdrawal cannot be satisfied exactly.
Design an ATM Machine Python/Java solution
Implement an ATM class with an internal array storing note counts. The deposit method adds counts directly to the array, while withdraw iterates from the largest denomination to the smallest using a greedy calculation. The same logic works across Python, Java, C++, C#, and JavaScript.
How to solve Design an ATM Machine in O(1)?
Maintain an array storing counts for the five denominations. For withdrawal, iterate from the largest denomination to the smallest and greedily take as many notes as possible without exceeding the remaining amount. If the remaining amount becomes zero, update the counts; otherwise return [-1]. Because only five denominations are processed, the algorithm runs in constant time.
What is the best approach for Design an ATM Machine?
The greedy approach using an array is the most efficient solution. Store the count of each denomination in a fixed array and process withdrawals from the largest denomination (500) down to the smallest (20). This guarantees the maximum-value notes are used first and runs in O(1) time because only five denominations are processed.
Is Design an ATM Machine asked at Google/Amazon/Meta?
Design-style data structure problems like this frequently appear in interviews at large tech companies. Variants of banking system simulations, resource allocation, or cashier systems have been reported in interviews at Amazon and other large-scale backend teams.
What data structure is used in Design an ATM Machine?
The most common implementation uses a fixed-size array to store counts of banknotes for denominations [20, 50, 100, 200, 500]. Some solutions use a hash map mapping denomination values to counts, but an array is simpler and more efficient because the set of denominations is fixed.
What is the time complexity of Design an ATM Machine?
Both deposit and withdraw operations run in O(1) time. The ATM supports exactly five banknote denominations, so every operation loops through a constant-size array. Space complexity is also O(1) because only five counters are stored.

Ready to solve this problem?

Practice Design an ATM Machine with our built-in code editor and test cases.

Practice on FleetCode