Five-Minute Hit Counter
Updated
Problem statement
Simulate a hit counter over a chronological operation sequence. Each operation has an integer type and timestamp:
- Type
0records one hit at that timestamp. - Type
1queries how many recorded hits occurred during the trailing300seconds.
For a query at timestamp t, count hits whose timestamps lie in the half-open interval (t - 300, t]. Multiple hits at the same timestamp count separately. Return the query results in encounter order.
Interview follow-ups
- Arbitrary windows: discuss how the structure changes when a query supplies any positive window length
kinstead of the fixed value300. - Distributed scale: discuss how to aggregate timestamp buckets across shards when one machine cannot retain or process the event volume.
The function you write uses only the fixed 300-second rule above.
Function
countRecentHits(operationTypes: int[], timestamps: int[]) → int[]Examples
Example 1
operationTypes = [0,0,0,1,1]timestamps = [1,2,3,4,300]return = [3,3]At timestamp 4, all three hits are recent. At timestamp 300, the hit at timestamp 1 is still inside (0, 300], so the count remains 3.
Example 2
operationTypes = [0,0,1,0,1]timestamps = [1,1,1,300,301]return = [2,1]The first query counts both hits at timestamp 1. At timestamp 301, those hits lie exactly on the excluded left boundary, while the hit at timestamp 300 remains.
Example 3
operationTypes = [1,0,1]timestamps = [100,400,400]return = [0,1]The first query occurs before any hit. The hit recorded at timestamp 400 is immediately visible to the following query at the same timestamp.
Constraints
1 <= operationTypes.length == timestamps.length <= 200000.- Every operation type is
0or1. 1 <= timestamps[i] <= 10^9.timestampsis nondecreasing.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Recent Hit CounterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Music Player Operation SequenceAtlassian · PHONE SCREENAtlassian interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Implement a FIFO QueueOptiver · PHONE SCREENOptiver interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Round-Robin Iterator over IteratorsBridge · ONSITE INTERVIEWBridge interview questions