Problem · Array

Maximum Requests in a Time Window

EasyIBMOA
See IBM hiring insights

Given an array of timestamps and a window size, find the maximum number of requests made within any time window defined as the closed interval [x, x + window - 1] for some integer x.

The function maximumRequests will take two inputs:

  • int window: the size of the time window
  • int timestamps[n]: array of request timestamps

The function should return an integer denoting the maximum number of requests made within any given window size.

Examples
01 · Example 1
window = 5
timestamps = [1, 2, 3, 8, 10]
return = 3

The window [0, 4] contains 3 requests at timestamps 1, 2, and 3, which is the maximum among all windows of size 5. Hence, the answer is 3.

Constraints
  • 1 <= window <= 10^9
  • 1 <= n <= 2 * 10^5
  • 0 <= timestamps[i] <= 10^9
  • It is guaranteed that timestamps are sorted in non-decreasing order.
More IBM problems
drafts saved locally
public int maximumRequests(int window, int[] timestamps) {
  // write your code here
}
window5
timestamps[1, 2, 3, 8, 10]
expected3
checking account