Issue Non-repeating OTPs in a Time Window
Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Mygate OA reported in August 2019 looks like a simple counter problem until the boundary hits you. You issue four-digit codes, smallest available first, and a code comes back exactly at t + window, not a tick before. That off-by-one is where the naive solution dies. The real shape is a min-heap of free codes plus a queue of codes waiting to expire, and it's a clean simulation once you see it. If you blank on the release ordering in the live assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the structure.
The problem
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 = 5 return = [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 = 3 return = [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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two structures. Keep a min-heap of available codes, seeded with 1000 through 9999. Keep a FIFO queue of (expiryTime, code) pairs. Since requestTimes is nondecreasing, expiries are also nondecreasing, so a plain queue works and you don't need a second heap. For each request at time t, pop every queue entry with expiry <= t and push its code back into the heap. Then pop the smallest code, record it, and enqueue (t + window, code). The pitfall is using < instead of <= at the boundary. Example 1 shows code 1000 reused at time 5 with window 5. Another trap is scanning all 9000 codes per request, which is wasteful. Equal timestamps need no special handling, because the heap hands out distinct codes in order. Total cost is O(n log 9000). StealthCoder is your hedge in the live OA if the expiry order slips your mind mid-run.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Issue Non-repeating OTPs in a Time Window cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Mygate's OA.
Mygate reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Issue Non-repeating OTPs in a Time Window FAQ
What's the trick in the Mygate OTP window problem?+
Pair a min-heap of free codes with a FIFO queue of pending expiries. Before each request, release every code whose expiry is at or before the current time. Then take the smallest from the heap. Nondecreasing times keep the queue sorted for free.
Why does the boundary matter so much here?+
A code issued at t is unavailable in [t, t + window), so it's free exactly at t + window. Use expiry <= t when releasing. A strict less-than fails Example 1, where code 1000 must come back at time 5.
How do equal timestamps work?+
They need no special case. Process them in input order, and each one pops the next smallest free code from the heap. Example 2 shows [4,4,4] giving 1000, 1001, 1002, and then 1000 again at time 7.
Is a plain array scan fast enough?+
Scanning 9000 codes per request with up to 1000 requests is about 9 million checks, which probably passes. But the heap plus queue is cleaner and safer. It's also what a reviewer expects to see.
How do I prepare for this in 48 hours?+
Write the heap plus queue version once from scratch. Test the two examples and an empty list. Then test a case where expiry equals the current time. That boundary is the whole problem, so rehearse it until it's automatic.