ATM queue exit order
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Google. It tests queue, simulation and sorting.
There are n people waiting in a single line at an ATM. They are numbered 1 to n from the front of the line to the back.
You are given an integer array amounts, where amounts[i] is the total money that person i + 1 wants to withdraw, and an integer k, the most the ATM pays out in one turn.
The line works like this:
- The person at the front steps up and withdraws
min(remaining, k), whereremainingis what they still need. - If they now have their full amount, they leave the line.
- Otherwise they go to the back of the line and wait for another turn.
Return a list of the people's numbers in the order in which they leave the line.
Implement atm_queue(amounts: list[int], k: int) -> list[int].
Constraints
0 <= n <= 10^51 <= amounts[i] <= 10^91 <= k <= 10^9
Example 1
amounts = [5, 1, 4], k = 2
Output: [2, 3, 1]
Person 1 takes 2 and goes to the back (needs 3). Person 2 takes 1 and leaves. Person 3 takes 2 and goes to the back (needs 2). Person 1 takes 2 (needs 1). Person 3 takes 2 and leaves. Person 1 takes 1 and leaves.
Example 2
amounts = [10, 10, 10], k = 4
Output: [1, 2, 3]
Everyone needs three turns, so they finish in the same round in their original order.
Clarifying questions
- Is
n = 0possible, and should it return[]? - Does a person who needs exactly
kleave after one turn? - Is a simulation acceptable, given that amounts can be up to
10^9andkcan be 1? - Are the people numbered from 1 in the output, as in the examples?
What a strong answer covers
- Sees that person
ineedsturns_i = ceil(amounts[i] / k)turns, and the queue is round-robin, so a person with fewer turns leaves in an earlier round. - Sorts people by
(turns_i, original index). That order is the exit order, because within one round people are served in their original order. That is O(n log n), and it doesn't depend on the size of the amounts. - Explains why a direct queue simulation is O(total turns), which can be about
10^14whenk = 1. - Uses integer ceiling division (
(a + k - 1) // k), not floating point. - Tests: both examples,
klarger than every amount (the original order), everyone needing the same number of turns,k = 1, and a check against a direct simulation on small inputs.
Follow-ups
- Return the time (number of turns served) when each person leaves.
- The ATM has a limited amount of cash and stops when it runs out: who leaves without their full amount?
- New people join the line at given times.
- There are several ATMs serving the same line.
Where it's asked
What it tests
queue · simulation · sorting · math
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
