KatInterview

Sliding Log Window: Distinct Tags and Dominant Tag Since a Cutoff

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designdesignhash-mapbinary-search

Last updated

Replay operations on a bounded, timestamp-ordered log of the newest entries, answering cutoff queries with the distinct key count and the most frequent key (smallest on ties).

Sliding Log Window

A log keeps only the newest capacity entries of a stream. Each entry has an integer timestamp and a string key. Entries are recorded in non-decreasing timestamp order. Whenever recording a new entry would leave the log with more than capacity entries, the entry that was recorded earliest (the oldest by arrival order) is discarded.

You are given a batch of operations to replay, in order, against an initially empty log. Return the answer to every query.

Operations

Each operation is a name plus an argument list.

  • "record_event" with arguments [timestamp, key]: append a new entry with this timestamp and key to the log. Then, if the log holds more than capacity entries, discard the single oldest one. This operation produces no output.

  • "query" with arguments [min_timestamp]: consider only the entries currently in the log whose timestamp is greater than or equal to min_timestamp (entries whose timestamp equals min_timestamp do qualify). Produce the two-element list [distinct_count, top_key]:

    • distinct_count is the number of different keys among the qualifying entries.
    • top_key is the key that occurs the most times among the qualifying entries (each qualifying entry counts once, so repeated keys add up). If several keys share the highest count, top_key is the smallest of them in standard string (lexicographic) order.
    • If no entry in the log qualifies (including when the log is empty), the answer is [0, ""].

    A query does not change the log.

Function

Implement replay_log(capacity, operations, arguments):

  • capacity (int): the maximum number of entries the log retains.
  • operations (List[str]): operation names, each either "record_event" or "query".
  • arguments (List[List]): same length as operations; arguments[i] is the argument list for operations[i], either [timestamp, key] (an int and a str) or [min_timestamp] (an int).

Return a list with one [distinct_count, top_key] item (an int and a str) per "query" operation, in the order the queries appear. If there are no queries, return an empty list.

The standard library is all you need.

Constraints

  • 1 <= capacity <= 10^5
  • 1 <= timestamp <= 10^9, and timestamps are non-decreasing across the record_event operations
  • key consists of 1 to 32 lowercase English letters
  • At most 2 * 10^5 record_event operations
  • At most 2000 query operations
  • Several entries may share the same timestamp
  • min_timestamp is an arbitrary integer, possibly below or above every retained timestamp
  • A query issued before any entry has been recorded returns [0, ""]

Examples

Example 1

Input: replay_log(3, ['record_event', 'record_event', 'record_event', 'query', 'record_event', 'query', 'query'], [[10, 'apple'], [20, 'banana'], [30, 'apple'], [15], [40, 'cherry'], [10], [50]])

Output: [[2, 'apple'], [3, 'apple'], [0, '']]

Hints

Hint 1

Because timestamps never decrease, the entries in the log are sorted by time, so the entries at or after a cutoff form a contiguous suffix of the log.

Hint 2

A deque or a list with a moving head index drops the oldest entry in O(1). For each query you can count keys in the qualifying suffix with a hash map; with at most 2000 queries this is fast enough.

Hint 3

To pick the top key, compare by (-count, key) so that the highest count wins and ties go to the lexicographically smallest key. Entries equal to the cutoff do count.