Overview
Find the busiest window of an API's request log, the number a rate limiter checks, with a window that grows and shrinks as two pointers move forward.
- a window that grows and shrinks
- each pointer only moves forward: O(n)
- less than 60 s apart is a strict bound
Short Course
A window can grow and shrink: move the right pointer over each item, and move
the left one forward while the window is too wide. Both only move forward, so
it's O(n), not O(n²). Trap: decide if the bound is < or <=.
left = 0
for right in range(len(times)): # sorted
while times[right] - times[left] > 10:
left += 1
print(times[left:right + 1]) # within 10 s
Task
Write busiest_window(times, seconds): the most requests in one window.
times is sorted; two requests share a window when they are less than
seconds apart. Example: busiest_window([1, 2, 3, 10, 11], 5) → 3.
Test Suites
Pending
Next
Output