Quick 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.

Find the maximum request throughput in any k-second window

Company: Citadel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an integer array `requests` where `requests[i]` is the number of requests a server received during second `i`. You are also given an integer `k`, the length of a contiguous time window in seconds. The **throughput** of a window is the total number of requests handled during that window. Find the maximum throughput over any contiguous window of exactly `k` seconds and return it. ### Function Signature ```python def max_throughput(requests: list[int], k: int) -> int: ``` ### Constraints - `1 <= k <= len(requests) <= 10^5` - `0 <= requests[i] <= 10^4` ### Examples ```text Input: requests = [1,3,2,6,4,8,5], k = 3 Output: 18 ``` The windows of length 3 sum to 6, 11, 12, 18 and 17. The largest is `[6, 4, 8]`, which sums to 18. ```text Input: requests = [5,1,2,7,3,4], k = 2 Output: 10 ``` The windows of length 2 sum to 6, 3, 9, 10 and 7, so the best window is `[7, 3]`. The window `[2, 7]` reaches only 9. ```text Input: requests = [2,9,1], k = 3 Output: 12 ``` When `k` equals the array length there is exactly one window, the whole array, and its sum is 12.

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.

You are given an integer array `requests`, where `requests[i]` is the number of requests a server received during second `i`, and an integer `k`, the length of a contiguous time window in seconds. The **throughput** of a window is the total number of requests received during that window, which is the sum of its elements. Look at every contiguous window of exactly `k` seconds, meaning every slice `requests[i..i+k-1]` for a start index `i` from `0` to `len(requests) - k`. Return the maximum throughput among those windows. Several windows can share the maximum throughput. The function returns only that maximum value, so a tie does not change the answer. Implement `max_throughput(requests, k)`. ### Constraints - `1 <= k <= len(requests) <= 10^5` - `0 <= requests[i] <= 10^4` Every window sum is at most `10^5 * 10^4 = 10^9`, which never exceeds 2^31 - 1, so the answer fits in a signed 32-bit integer and every language returns a plain `int`. ### Example 1 ```text Input: requests = [1,3,2,6,4,8,5], k = 3 Output: 18 ``` The windows of length 3 sum to 6, 11, 12, 18 and 17. The largest is `[6, 4, 8]`, which sums to 18. ### Example 2 ```text Input: requests = [2,9,1], k = 3 Output: 12 ``` When `k` equals the array length there is exactly one window, the whole array, and its sum is 12.

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

  1. 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.
  2. 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.
  3. Several windows may tie for the maximum. Only the value is returned, so you never need to choose between them.

Peak Server Throughput Over a Fixed Window

easyCitadelSoftware Engineer

You are given an integer array requests, where requests[i] is the number of requests a server received during second i, and an integer k, the length of a contiguous time window in seconds.

The throughput of a window is the total number of requests received during that window, which is the sum of its elements. Look at every contiguous window of exactly k seconds, meaning every slice requests[i..i+k-1] for a start index i from 0 to len(requests) - k. Return the maximum throughput among those windows.

Several windows can share the maximum throughput. The function returns only that maximum value, so a tie does not change the answer.

Implement max_throughput(requests, k).

Constraints

  • 1 <= k <= len(requests) <= 10^5
  • 0 <= requests[i] <= 10^4

Every window sum is at most 10^5 * 10^4 = 10^9, which never exceeds 2^31 - 1, so the answer fits in a signed 32-bit integer and every language returns a plain int.

Example 1

Input: requests = [1,3,2,6,4,8,5], k = 3
Output: 18

The windows of length 3 sum to 6, 11, 12, 18 and 17. The largest is [6, 4, 8], which sums to 18.

Example 2

Input: requests = [2,9,1], k = 3
Output: 12

When k equals the array length there is exactly one window, the whole array, and its sum is 12.

Examples

Example 1
Input
([1, 3, 2, 6, 4, 8, 5], 3)
Output
18
Notes
First source example: the windows sum to 6, 11, 12, 18 and 17, so the best is [6, 4, 8].
Example 2
Input
([5, 1, 2, 7, 3, 4], 2)
Output
10
Notes
Second source example: the windows sum to 6, 3, 9, 10 and 7, so the best is [7, 3].

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

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_throughput(requests: list[int], k: int) -> int:
Sample call
max_throughput([1, 3, 2, 6, 4, 8, 5], 3)
Language
Case 1
Input
([1, 3, 2, 6, 4, 8, 5], 3)
Expected
18
Case 2
Input
([5, 1, 2, 7, 3, 4], 2)
Expected
10

Browse More Questions

Show the approach

Approach

Use a sliding window. Sum the first k elements to get the throughput of the window that starts at index 0, and record it as the best so far. Then move the window one second to the right at a time: the window that ends at index i has a sum equal to the previous window's sum plus requests[i] minus requests[i - k], the element that just dropped out. After each step, compare the running sum with the best and keep the larger.

Invariant: after processing index i, the running sum equals the sum of requests[i - k + 1..i], and best is the maximum over every window that ends at or before index i. The loop visits every end index from k - 1 to n - 1, so each of the n - k + 1 windows is scored exactly once and the returned value is the true maximum. Ties need no handling because only the value is returned.

Edge cases: when k equals the array length the slide loop never runs and the whole array sum is returned; when k is 1 the answer is the largest element; an all-zero array returns 0. Every sum is at most 10^9, so a 32-bit int holds the answer, and the Java and C++ references still accumulate in 64-bit integers.

Time complexity:
O(n)
Space complexity:
O(1)