All problems
Sliding Window Counter
Problem
Simulate a sliding-window rate counter.
window_size is the window length and operations contains "hit" (record a hit at timestamps[i]) or "count" (return the hits with timestamps in (t - window_size, t]). Unlike a fixed window, the boundary moves with every query.
Return the list of recorded results (None for hit, an integer for count).
Hint
The window is open on the left and closed on the right: t - window_size < x <= t.
Test cases
case 1
in: [10,["hit","hit","hit","count","count"],[1,2,11,12,25]]
expected: [null,null,null,1,0]
case 2
in: [5,["hit","hit","count"],[1,6,6]]
expected: [null,null,1]
case 3
in: [5,["count"],[0]]
expected: [0]
case 4
in: [3,["hit","count"],[4,4]]
expected: [null,1]
Starter codePython
def sliding_window_counter(window_size, operations, timestamps):
# Your code here
pass4 test cases · run your code against them in the editor