FastPrepWait Time for the Next Customer

Wait Time for the Next Customer

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

There are n servers. Server i takes exactly serviceTimes[i] time units for every customer. All servers are idle at time 0, and waitingPeople customers are ahead of you in one first-come, first-served queue.

Whenever a server is free and the queue is nonempty, the next customer starts immediately. Return the earliest time at which you start service. If a server is already available for you at time 0, return 0.

When several servers finish at the same time, all of those service slots become available at that time.

Function

waitTimeForNextCustomer(serviceTimes: int[], waitingPeople: long) → long

Examples

Example 1

serviceTimes = [5,10]waitingPeople = 0return = 0

Both servers are idle, so you start immediately.

Example 2

serviceTimes = [5,10]waitingPeople = 2return = 5

The two waiting customers start at time zero. The five-unit server frees first.

Example 3

serviceTimes = [3,7]waitingPeople = 5return = 9

Customers ahead start at times 0,0,3,6,7; you take the next slot at time 9.

Constraints

  • 1 <= serviceTimes.length <= 100000.
  • 1 <= serviceTimes[i] <= 1000000.
  • 0 <= waitingPeople <= 10^12.

More Google problems

See Google hiring insights
public long waitTimeForNextCustomer(int[] serviceTimes, long waitingPeople) {
    // Write your code here.
}
serviceTimes[5,10]
waitingPeople0
expected0
Checking account…