Find the maximum request throughput in any k-second window
Company: Citadel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
Overview: Given the number of requests a server receives each second and a window length k, find the largest total number of requests in any k consecutive seconds. It tests fixed-size sliding window reasoning, off-by-one care in the window update, and a linear-time solution with constant extra space.
Constraints
- 1 <= k <= len(requests) <= 10^5
- 0 <= requests[i] <= 10^4
- The answer is at most 10^9, which fits in a signed 32-bit integer
Examples
Input: ([1, 3, 2, 6, 4, 8, 5], 3)
Expected Output: 18
Explanation: First source example: the windows sum to 6, 11, 12, 18 and 17, so the best is [6, 4, 8].
Input: ([5, 1, 2, 7, 3, 4], 2)
Expected Output: 10
Explanation: Second source example: the windows sum to 6, 3, 9, 10 and 7, so the best is [7, 3].
Hints
- There are exactly len(requests) - k + 1 windows, one for each start index from 0 to len(requests) - k. Make sure both the first and the last window get scored.
- With len(requests) and k both up to 10^5, adding up all k elements of every window separately can take around 10^10 steps. Look for a way to get each window's sum without re-adding all of its elements.
- Several windows may tie for the maximum. Only the value is returned, so you never need to choose between them.