AlgoMaster Logo
AlgoMasterSchedule Connection Pool Waitershard

Schedule Connection Pool Waiters

hard

A database connection remains occupied while its request runs. When the pool is full, callers wait in arrival order, but a caller whose wait exceeds the configured timeout gives up without consuming a connection.

Design a ConnectionPoolScheduler class:

  • ConnectionPoolScheduler() creates a stateless scheduler.
  • int[] startTimes(int[] arrivals, int[] durations, int poolSize, int waitTimeout) returns the start time of every request, or -1 when that request times out.

Requests are supplied in nondecreasing arrival order and are processed first-come, first-served. A connection that starts a request at time t for duration d becomes free at t + d. A request may wait at most waitTimeout; waiting exactly that long is allowed. Timed-out requests do not occupy a connection.

Example 1:

Input:

Output:

Explanation: Two connections serve the first requests immediately. The third waits from 1 to 3, and the final request begins at its arrival time 5.

Example 2:

Input:

Output:

Explanation: The only connection is unavailable until time 10, beyond both later requests' deadlines.

Constraints

  • 0 <= arrivals.length == durations.length <= 10^5
  • 0 <= arrivals[i] <= 10^9, in nondecreasing order.
  • 1 <= durations[i] <= 10^6
  • 1 <= poolSize <= 10^5
  • 0 <= waitTimeout <= 10^9
  • Every computed finish time fits in a signed 32-bit integer.
Hints

Loading...
CallReturns
new ConnectionPoolScheduler()null
startTimes([0,0,1,5], [4,3,2,1], 2, 2)[0,0,3,5]

Two requests start immediately. The third waits from time 1 to 3, exactly two units, and the fourth starts immediately at its arrival time 5.

Run checks these cases. Submit also runs a larger hidden set.