Count Events in Rolling Minute Windows
Problem statement
Implement a counter service that records bump() events by minute and answers query() with the number of events in a rolling 60-minute window, accurate to one minute. Expired minute buckets must stop contributing as time advances. Do not use a stack; use constant-size counter state for the fixed 60-minute window.
Practice contract
You receive two nondecreasing arrays of integer minute indices: eventMinutes contains one entry for every bump, and queryMinutes contains the query times. Replay them on one chronological timeline. Before a query at minute t, record every event with timestamp at most t. Events later than the query are not recorded early.
For each query, return the number of events with minute indices in the inclusive interval [t - 59, t], in query order. Repeated events at the same minute count separately, and all events at a query minute are recorded before that query. Repeated queries do not change the count. Return an empty array when there are no queries.
For example, events at 1:01, 1:02, and 1:15 correspond to minute indices 61, 62, and 75. A query at 1:10 (70) returns 2; the future 1:15 event is excluded. At 2:05 (125), the first two events have expired and the count is 1.
Function
countMinuteEvents(eventMinutes: int[], queryMinutes: int[]) → int[]Examples
Example 1
eventMinutes = [61,62,75]queryMinutes = [70,125]return = [2,1]At minute 70, only events 61 and 62 have occurred. At minute 125, the window is [66,125], so only event 75 remains.
Example 2
eventMinutes = [0,0,59,60]queryMinutes = [0,59,60]return = [2,3,2]Both events at minute 0 are visible to the first query. At minute 60, minute 0 is outside [1,60], while events 59 and 60 remain.
Example 3
eventMinutes = []queryMinutes = [0,1000000000]return = [0,0]No events are recorded, so both queries return zero.
Constraints
- For this exercise, assume
0 <= eventMinutes.length, queryMinutes.length <= 100000. - For this exercise, assume every minute index is in
[0, 10^9]and each array is nondecreasing. - For this exercise, assume equal-minute events precede queries and the window contains the current minute and the previous 59 minute buckets.
- The fixed 60-minute service state must use
O(1)space, excluding the supplied arrays and returned results. Each bump and query must useO(1)time for this fixed window.