Problem · Array

Maximum Requests in Window

Learn this problem
MediumSalesforce logoSalesforceFULLTIMEOA
See Salesforce hiring insights

Problem statement

Given a series of request timestamps, find the maximum number of requests that occur within any continuous time window of a specified length.

The function receives:

  • timestamp: request timestamps in minutes
  • windowSize: duration of the time window in minutes

Return the maximum number of requests observed in any window of length windowSize minutes.

Function

maxRequestInWindow(timestamp: int[], windowSize: int) → int

Examples

Example 1

timestamp = [1,3,7,5]windowSize = 4return = 2

Windows such as [1,4], [3,6], and [5,8] each contain two requests. The maximum is 2.

Example 2

timestamp = [2,2,3]windowSize = 1return = 2

The window [2,2] includes both requests at time 2.

Constraints

  • 1 <= timestamp.length <= 2 * 10^5
  • 1 <= timestamp[i] <= 10^9
  • 1 <= windowSize <= 10^9

More Salesforce problems

drafts saved locally
public int maxRequestInWindow(int[] timestamp, int windowSize) {
  // write your code here
}
timestamp[1,3,7,5]
windowSize4
expected2
checking account