Peak Request Load Over a Fixed Run of Minutes
Arrays & HashingAuto-graded · 20 test casesLast updated
Find the largest total of any k consecutive values in a list, testing fixed-size sliding window technique and the ability to avoid recomputing overlapping sums.
Peak Request Load Over a Fixed Run of Minutes
A service logs how many requests it receives during each minute, in chronological order. You want to know the busiest stretch of exactly k back-to-back minutes.
Write a function that takes the per-minute request counts and the stretch length k, and returns the greatest total number of requests found in any block of k consecutive minutes.
Details
- A block is a run of
kneighboring entries in the list, with no gaps and no reordering. - Blocks may overlap with each other. Every starting position that leaves room for
kentries must be considered. - Return only the total, as a single integer. You do not need to report where the block begins.
- If several blocks tie for the largest total, the answer is simply that shared total.
- If
kequals the length of the list, the only block is the whole list, so return the sum of all entries. - If
kis 1, the answer is the largest single entry.
Constraints
countsis a list of positive integers.1 <= k <= len(counts)- The input is always valid, so you do not need to validate
k. - Aim for time proportional to
len(counts), rather than recomputing every block's sum from scratch.
Examples
Example 1
Input: peak_window_total([1, 2, 3, 4, 5], 2)
Output: 9
Example 2
Input: peak_window_total([2, 1, 5, 1, 3, 2], 3)
Output: 9
Example 3
Input: peak_window_total([5, 5, 5, 5], 2)
Output: 10
Hints
Hint 1
Adding up every block separately repeats a lot of work, because neighboring blocks share all but one element on each side.
Hint 2
When the block shifts one position to the right, exactly one value enters on the right and exactly one value leaves on the left. Update the running total using only those two values.
Hint 3
Compute the total of the first k entries to start, then slide across the rest of the list while tracking the largest total seen. This needs only a few variables beyond the input.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Summing each block from scratch costs O(n * k), because neighboring blocks overlap in k - 1 entries. Instead, use a fixed-size sliding window with a running total.
- Add up the first
kentries. This is the first block's total, and it is the initial best. - For each index
ifromkton - 1, move the block one step right. The entrycounts[i]joins the block and the entrycounts[i - k]leaves it, sowindow_sum += counts[i] - counts[i - k]. - After each shift, compare the new total with the best so far and keep the larger one.
- Return the best total.
Why it is correct
At the start of the iteration for index i, window_sum equals the sum of counts[i-k : i], and best is the maximum over all blocks that end before i. Initially this holds because the only block seen is the first one. Each shift replaces the block ending at i - 1 by the block ending at i, which is exactly the update made, and the comparison keeps best correct. When the loop ends, all n - k + 1 blocks have been examined exactly once, so best is the true maximum. The argument never uses positivity, so the method also works with zeros or negatives.
Edge cases follow naturally: when k == len(counts) the loop never runs and the full sum is returned; when k == 1 each block is a single entry, so the result is the maximum entry; ties do not matter because only the total is returned.
Complexity
- Time: O(n). The initial sum takes O(k) and the loop does O(1) work for each of the remaining n - k positions.
- Space: O(1) extra, just the running total, the best total, and a loop index.
Reference solution
from typing import List
def peak_window_total(counts: List[int], k: int) -> int:
window_sum = 0
for i in range(k):
window_sum += counts[i]
best = window_sum
for i in range(k, len(counts)):
window_sum += counts[i] - counts[i - k]
if window_sum > best:
best = window_sum
return best