Katkat

Count Hits in the Past Five Minutes

VercelSoftware Engineer · Technical Screen

Last updated

Implement a five-minute hit counter with ordered timestamped operations, repeated hits, and an explicit 300-second expiration boundary.

Implement a hit counter that reports how many hits occurred during the past five minutes.

For this exercise, timestamps are integer seconds, operations arrive in nondecreasing timestamp order, and a query at time t counts hits with timestamps in (t - 300, t]. These boundary and ordering conventions make the five-minute task deterministic.

Function Signature

count_recent_hits(operations: list[list[int]]) -> list[int]

Input

Each operation is a pair [kind, timestamp]:

  • kind = 0: record one hit at timestamp.
  • kind = 1: query the hit count at timestamp.

Only hits recorded by earlier operations are visible to a query. Several hits and queries may share the same timestamp; their input order still matters.

Output

Return one integer for each query, in query order. A hit exactly 300 seconds before a query is excluded. If there are no queries, return an empty list.

Constraints

  • 0 <= len(operations) <= 200000.
  • 0 <= timestamp <= 1000000000.
  • Timestamps are nondecreasing across all operations.
  • Each recorded hit counts once. No other event types occur.

Examples

Input: operations = [[0,1],[0,2],[0,300],[1,300],[1,301]]

Output: [3,2]

At time 300, the hit at time 1 is included. At time 301, that hit lies exactly at the excluded lower boundary.

Input: operations = [[1,5],[0,5],[1,5],[0,5],[1,5]]

Output: [0,1,2]

Input: operations = []

Output: []