KatInterview

Sliding Five-Minute Hit Tally for Each Caller

Data Structure DesignAuto-graded · 27 test cases
EasyAuto-gradedData Structure Designhash-mapqueuesliding-window

Last updated

Process a batch of hit and query operations, returning each caller's number of hits within an inclusive 300-second window, using per-caller queues with lazy expiry.

You are building a small usage tracker that counts how many requests each caller has made recently.

You are given two parallel lists, operations and args, describing a sequence of calls to be processed in order. The i-th operation is operations[i], and its parameters are in args[i], which is always a two-element list [caller_id, timestamp].

There are two kinds of operations:

  • "hit": record one request made by caller_id at time timestamp (in seconds).
  • "getHits": report how many requests caller_id has made within the last 5 minutes as of timestamp. This means every recorded hit of that caller whose time lies in the inclusive range [timestamp - 299, timestamp].

Hits from different callers are tracked completely independently of one another.

Return a list with one entry per operation, in the same order:

  • None for every "hit".
  • The integer count for every "getHits".

Rules and edge cases

  • Timestamps never decrease over the whole sequence of operations. Several operations, including several hits from the same caller, may share the same timestamp, and each hit counts separately.
  • A hit made exactly at timestamp - 299 is counted. A hit made at timestamp - 300 or earlier is not.
  • A "getHits" for a caller that has never made a hit returns 0.
  • Hits that are too old to matter for any future query may be discarded; the design should avoid scanning all callers on each operation, and should aim for amortized constant time per operation.

Constraints

  • len(operations) == len(args)
  • Each operation is either "hit" or "getHits".
  • caller_id is a string; timestamp is a non-negative integer.
  • Timestamps are non-decreasing across the entire sequence.
  • Many distinct callers may appear, and most of them may be idle at any moment.

Examples

Example 1

Input: caller_hit_tally(['hit', 'hit', 'hit', 'getHits', 'getHits'], [['a', 1], ['a', 2], ['b', 3], ['a', 4], ['b', 4]])

Output: [None, None, None, 2, 1]

Example 2

Input: caller_hit_tally(['hit', 'getHits', 'hit', 'getHits'], [['a', 0], ['a', 300], ['a', 300], ['a', 300]])

Output: [None, 0, None, 1]

Example 3

Input: caller_hit_tally(['hit', 'hit', 'hit', 'getHits'], [['a', 10], ['a', 10], ['a', 10], ['a', 10]])

Output: [None, None, None, 3]

Hints

Hint 1

Keep a separate structure for each caller, keyed by caller id in a dictionary, so one caller's traffic never affects another's count.

Hint 2

Because timestamps never decrease, each caller's list of hit times is already sorted, so the oldest hits are always at the front of a queue.

Hint 3

When answering a query, remove from the front while the time is less than timestamp - 299, then the queue length is the answer. Each hit is removed at most once, giving amortized O(1). Do not create an entry when querying an unknown caller.