Maximum tests in one activation window
A shared test environment receives test cases over time. You are given an integer array arrivalTime, where each entry is the time one test case arrives. Several test cases can arrive at the same time.
You may turn the environment on once, at time t1, and turn it off at time t2 (t1 <= t2). Running it costs budget, so it may stay on for at most k time units: t2 - t1 <= k. Every test case whose arrival time lies in the closed interval [t1, t2] runs. Tests take no time, and tests that arrive together run together.
Implement:
def max_tests(arrival_time: list[int], k: int) -> int
Return the largest number of test cases that a single activation can run.
Constraints
0 <= len(arrivalTime) <= 10^5-
-10^9 <= arrivalTime[i] <= 10^9. The array is not sorted. 0 <= k <= 2 * 10^9
Example 1
arrivalTime = [8, 2, 5, 14, 6], k = 4
Output: 3
Turn on at 2 and off at 6. The tests at 2, 5 and 6 run. No interval of length 4 holds four tests.
Example 2
arrivalTime = [3, 3, 3, 9, 11], k = 1
Output: 3
The three tests at time 3 run together, for example with [3, 4]. The tests at 9 and 11 are 2 apart, so at most one of them fits with k = 1.
Be ready to extend the solution: return the chosen (t1, t2), or allow up to m separate activations.
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
