Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-192

Sliding Window Aggregation Struct

Problem

Maintain a sliding window that answers sum and distinct-count queries.

operations contains "push" (append values[i]), "pop" (remove the oldest element), "sum" (record the total), or "distinct" (record the number of distinct values). Keep a dictionary of value counts alongside the running total.

Return the list of recorded results (None for push and pop).

Hint

Deleting a dictionary entry when its count hits zero keeps the distinct count exact.

Test cases

case 1
in: [["push","push","push","sum","distinct","pop","sum","distinct"],[2,2,3,0,0,0,0,0]]
expected: [null,null,null,7,2,null,5,2]
case 2
in: [["sum","distinct"],[0,0]]
expected: [0,0]
case 3
in: [["push","pop","sum"],[5,0,0]]
expected: [null,null,0]
case 4
in: [["push","push","pop","distinct"],[1,2,0,0]]
expected: [null,null,null,1]
Starter codePython
def window_aggregates(operations, values):
    # Your code here
    pass

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