Google | Phone | Time for turn
Anonymous User
4655

Problem description
Joseph is standing at k+1th position in a queue at an insurance company's office and there are n counters at the office, ith counter takes time[i] time (minutes) to process a request. The security guard assigns a counter to the person standing in the front of the queue as soon as a counter is available or if multiple counters are available, then the security official assigns the counter with minimum id (consider id as index). What would be the time at which Joseph's request would be processed aka the ending time when Joseph leaves the office?

Example case

n = 3
times = [3,2,5]
k = 4

Person 1: assign counter 0 (available counter with minimum index)
Person 2: assign counter 1 (available counter with minimum index)
Person 3: assign counter 2 (available counter with minimum index)
Person 4: assign counter 1 (available counter with minimum index)
Person 5 (Joesph): assign counter 0 (available counter with minimum index)

Solution proposed

  • Maintain ending times for each counter in a priority-queue (min heap)
    • Initially add n counters to the queue with ending time as 0
    • Custom comparator function to handle counters with same ending times (choose the one with minimum index)
  • Iterate for each person in the queue for k positions:
    • Pick the counter to be assigned from the heap and re-insert it with updated end-time
  • Fetch the top most element from the heap and answer would be endTime + time taken to process request for that counter

Interview Experience Post: https://leetcode.com/discuss/interview-question/2199510/Google-or-L4-or-India-or-June-2022-or-Accepted

Comments (10)