Count Hits in the Past Five Minutes
Company: Vercel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Implement a five-minute hit counter with ordered timestamped operations, repeated hits, and an explicit 300-second expiration boundary.
Constraints
- 0 <= len(operations) <= 200000.
- Each operation is a pair [kind, timestamp] with kind in {0, 1}: kind = 0 records one hit at timestamp, kind = 1 queries the hit count at timestamp.
- 0 <= timestamp <= 1000000000.
- Timestamps are nondecreasing across all operations.
- Each recorded hit counts once. No other event types occur.
- Only hits recorded by earlier operations are visible to a query; hits and queries sharing a timestamp are processed in input order.
- A query at time t counts hits with timestamps in (t - 300, t]; a hit exactly 300 seconds before the query is excluded.
- Each returned count is at most 200000, so every value fits in a signed 32-bit integer.
Examples
Input: ([],)
Expected Output: []
Explanation: Minimum valid input: no operations at all, so there are no queries and the result is empty.
Input: ([[1, 0]],)
Expected Output: [0]
Explanation: Single query at time 0 with no prior hits; the window (-300, 0] holds nothing.
Hints
- The operations are a replay: walk the list once in order, and remember that a query can only see hits that appeared earlier in the list, even when they share the same timestamp.
- Timestamps never decrease, so a hit that is already too old for one query is also too old for every query that comes after it.
- Re-read the boundary rule before you write the comparison: a hit exactly 300 seconds before the query is outside the window, while one 299 seconds before is inside.