FastPrepIssue Non-repeating OTPs in a Time Window

Issue Non-repeating OTPs in a Time Window

Mygate logoMygate● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a nondecreasing integer array requestTimes and a positive integer window, issue one four-digit integer code for each request. Codes range from 1000 through 9999.

At each request, choose the smallest available code. A code issued at time t is unavailable during the half-open interval [t, t + window) and becomes available again exactly at t + window. Every reuse starts a new interval.

Process requests with equal timestamps in their input order. Return the issued codes in request order. The request-count bound guarantees that a code is always available. An empty request list returns an empty array.

Function

issueOtps(requestTimes: int[], window: int) → int[]

Examples

Example 1

requestTimes = [0,1,2,5,6]window = 5return = [1000,1001,1002,1000,1001]

At time 5, code 1000 expires and becomes the smallest available code. Code 1001 similarly expires at time 6.

Example 2

requestTimes = [4,4,4,7]window = 3return = [1000,1001,1002,1000]

Equal-time requests receive distinct codes in input order. All three codes expire at time 7, when the smallest can be reused.

Constraints

  • 0 ≤ requestTimes.length ≤ 1000.
  • 0 ≤ requestTimes[i] ≤ 10^9.
  • requestTimes is nondecreasing.
  • 1 ≤ window ≤ 10^9.

More Mygate problems

See Mygate hiring insights
public int[] issueOtps(int[] requestTimes, int window) {
    // write your code here
}
requestTimes[0,1,2,5,6]
window5
expected[1000,1001,1002,1000,1001]
Checking account…