Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-158

Fixed Window Counter

Problem

Simulate a fixed-window rate counter.

window_size is the window length and operations contains "hit" (record a hit at timestamps[i]) or "count" (return the hits in the fixed window containing timestamps[i]). The window containing time t spans from (t // window_size) * window_size up to but not including start + window_size.

Return the list of recorded results (None for hit, an integer for count).

Hint

Integer division by the window size gives the window start.

Test cases

case 1
in: [10,["hit","hit","hit","count","hit","count"],[1,2,11,12,11,12]]
expected: [null,null,null,1,null,2]
case 2
in: [5,["count"],[0]]
expected: [0]
case 3
in: [5,["hit","count"],[7,7]]
expected: [null,1]
case 4
in: [5,["hit","hit","count"],[4,5,9]]
expected: [null,null,1]
Starter codePython
def fixed_window_counter(window_size, operations, timestamps):
    # Your code here
    pass

4 test cases · run your code against them in the editor