Member-only story

HackerRank Coding Interview 4. Queue at ATM

Amber Ivanna Trujillo
4 min readDec 8, 2022

--

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 :

  1. 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]

--

--

Amber Ivanna Trujillo
Amber Ivanna Trujillo

Written by Amber Ivanna Trujillo

I am Executive Data Science Manager. Interested in Deep Learning, LLM, Startup, AI-Influencer, Technical stuff, Interviews and much more!!!

Responses (1)