Member-only story
HackerRank Coding Interview 4. Queue at ATM
Time to complete —54 minutes
Question Type = Medium
Asked in = Microsoft, Uber, Lyft, Bloomberg, Morgan Stanley, Apple, and Google.
Skills: arrays, sorting, comparators
Insights — It should be read very carefully read and you must ask questions in between.
Question —
Given an array of integers, amount[n], each amount[i] represents the amount of money required by person i. There are n people numbered 1 through n standing in an ATM queue in increasing order. A person can withdraw at most k units of currency at one time. Return an array of people numbers in the order that they leave the queue, i.e. the order their needed amounts have been withdrawn.
Call the cumulative amount of money withdrawn current, and the money required total.
Repeat the following process until the queue is empty :
- If total — current ≤ k
- withdraw (total — current)
- leave the queue
- store the person’s i in the return array
2. Otherwise
- withdraw k
- current = current + k
- move to the back of the queue
Example
k = 2
n = 3
amount = [2, 5, 1]