AlgoMaster Logo
AlgoMasterAggregate Tumbling Windowsmedium

Aggregate Tumbling Windows

medium

Streaming systems divide unbounded event sequences into finite windows before computing aggregates. A tumbling window uses fixed-size, non-overlapping intervals, so each event belongs to exactly one window.

Design a TumblingWindowAggregator class:

  • TumblingWindowAggregator() creates a stateless aggregator.
  • int[] aggregate(int[] timestamps, int[] values, int windowSize) returns the sum of values in each touched range of the timeline.

The event at timestamps[i] has value values[i]. Windows begin at timestamp 0, and the event at time t belongs to window index:

where / is integer division. Return one sum for every window from index 0 through the highest window touched by any event. If no event belongs to a window inside that range, its sum must be 0.

Timestamps do not have to be sorted, and values may be negative.

Example 1:

Input:

Output:

Explanation: Window 0 covers timestamps 0 through 4 and sums 10 + 20 = 30. Window 1 covers timestamps 5 through 9 and sums 30 + 40 = 70.

Example 2:

Input:

Output:

Explanation: The events touch windows 0 and 2. Window 1 contains no events but must remain in the output with a sum of 0.

Constraints

  • 1 <= timestamps.length <= 1000
  • timestamps.length == values.length
  • 0 <= timestamps[i] <= 10^5
  • -10^4 <= values[i] <= 10^4
  • 1 <= windowSize <= 10^5
  • Every window sum fits in a signed 32-bit integer.
  • At most 100 calls are made to aggregate.
Hints

Loading...
CallReturns
new TumblingWindowAggregator()null
aggregate([0,1,5,6], [10,20,30,40], 5)[30,70]

Timestamps 0 and 1 belong to window 0, while 5 and 6 belong to window 1. Their value sums are 30 and 70.

Run checks these cases. Submit also runs a larger hidden set.