ATM queue exit order

0
Sign in

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), where remaining is 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^5
  • 1 <= amounts[i] <= 10^9
  • 1 <= 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.

GoogleCodingEasy

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.