Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Capacity planning wants to know when the API is under the most sustained load, not just its single busiest minute. The metrics job already rolls requests up to one count per minute; what it needs now is the stretch of k consecutive minutes with the highest total, so autoscaling can be sized for that burst.
Write busiest_window(per_minute, k). It takes per_minute, a list of non-negative integer request counts where index 0 is minute 0, and k, an integer window width in minutes. It returns a dict {"start_minute": int, "total": int} describing the busiest window of exactly k minutes.
Function to write
busiest_window(per_minute: list[int], k: int) -> dict | NoneA dict with the start minute and request total of the earliest busiest k-minute window, or None when no such window exists.
How to approach it
Two neighbouring windows share all but two minutes. Keep one running total and adjust it by what leaves and what enters.
Sample cases
+ 3 held back until you submit
a morning of traffic
Six windows of three minutes; the peak is the window starting at minute 1.
Input
Argument 1
[
3,
5,
2,
7,
1,
4,
6,
0
]Argument 2
3Returns
{
'start_minute': 1,
'total': 14
}the peak is the last window
The busiest pair is the final two minutes, which an off-by-one loop never reaches.
Input
Argument 1
[
1,
2,
1,
0,
3,
9
]Argument 2
2Returns
{
'start_minute': 4,
'total': 12
}every window ties
All four windows total 5, so the earliest start (minute 0) must win the tie.
Input
Argument 1
[
4,
1,
4,
1,
4
]Argument 2
2Returns
{
'start_minute': 0,
'total': 5
}an empty log
No minutes recorded means no window of any width exists, so the answer is None.
Input
Argument 1
[] (empty list)
Argument 2
2Returns
None
Constraints
k consecutive minutes: minutes start_minute to start_minute + k - 1 inclusive. The last valid window starts at len(per_minute) - k.{"start_minute": s, "total": t} where t is the largest window total and s is where that window starts.start_minute.None when k <= 0, when k is larger than the number of minutes, or when per_minute is empty.0, not missing.k.Worked example
Take [3, 5, 2, 7, 1, 4, 6, 0] with k = 3. The first window is minutes 0–2, [3, 5, 2], which sums to 10 — that sum is computed once. Slide right: minute 0 (3) leaves and minute 3 (7) enters, so the total is 10 - 3 + 7 = 14. Slide again: drop 5, add 1 → 10. Then drop 2, add 4 → 12; drop 7, add 6 → 11; drop 1, add 0 → 10. The best is 14, first reached by the window starting at minute 1, so the answer is {"start_minute": 1, "total": 14}.
The trap: a loop written as range(len(per_minute) - k) stops one window early. On [1, 2, 1, 0, 3, 9] with k = 2 the peak is the very last pair, 3 + 9 = 12 starting at minute 4, and that loop never looks at it.
What this tests
The fixed-size sliding window — LeetCode 'Maximum Average Subarray I' in metrics clothing. The target is O(n) time and O(1) extra space: re-summing each window is O(n·k), which passes these samples and crawls when the series is a month of seconds and k is an hour. Data engineers meet this for real computing peak-hour load, busiest rolling windows for SLO reports, and burst detection over metric series. Microsoft data engineering candidates report sliding-window and two-pointer problems in the Python round, though this exact problem is not named in the reports we could find; it is the standard Maximum Average Subarray shape and covers the fixed-width variant of the pattern.
busiest_window(per_minute: list[int], k: int) -> dict | NoneSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.