Recent Hit Counter
Updated
Problem statement
You are given a list of operations, in order. Operation i has type operationTypes[i] and integer timestamp timestamps[i], in seconds:
- Type
0records one hit at that timestamp. - Type
1asks how many recorded hits happened in the last 300 seconds.
For a query at timestamp t, count the hits whose timestamps are in (t - 300, t]. A hit exactly at the left end does not count, but a hit exactly at t does. Several hits at the same timestamp each count separately.
Return the answers to the type-1 operations, in the order those operations appear.
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]The query at timestamp 4 counts all three hits. For the query at timestamp 300, even the oldest hit, at timestamp 1, is still inside (0, 300]. So both queries return 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, so it returns two. The second query is at timestamp 301. Its window leaves out its left end, and those two hits sit exactly there, so they no longer count. Only the hit at timestamp 300 counts, so the second query returns one.
Example 3
operationTypes = [1,0,1]timestamps = [100,400,400]return = [0,1]The first query comes before any hit, so it returns zero. The hit recorded at timestamp 400 counts right away for the next query at that same timestamp, so that query returns one.
Constraints
1 ≤ operationTypes.length = timestamps.length ≤ 200000.- Every operation type is
0or1. 1 ≤ timestamps[i] ≤ 10^9.timestampsnever decreases: each value is greater than or equal to the one before it.
More Amazon Web Services Onsite Interview questions
- Group AnagramsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring with At Most K Distinct CharactersONSITE INTERVIEW · Seen Nov 2026
- Fixed-K Kth Largest StreamONSITE INTERVIEW · Seen Aug 2026
- Logger Rate LimiterONSITE INTERVIEW · Seen Aug 2026
- Spreadsheet Formula DependenciesONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Five-Minute Hit CounterGoogle · ONSITE INTERVIEWGoogle 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
- Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions