KatInterview

Ranking Frequent Event Names Within a Time Window

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designdesignbinary-searchhash-map

Last updated

Simulate a stream of timestamped log events and answer window queries for the k most frequent messages, with ties broken alphabetically, using binary search over a sorted log.

Event Frequency Ranking

You are building a small in-memory event recorder. Events arrive over time, each with an integer timestamp and a text label. At any point you may ask which labels were most common during a given time window.

The recorder is driven by a batch of commands. Write a function that processes the commands in order and returns one result per command.

Commands

You receive two lists of equal length:

  • operations: each entry is either "log_message" or "get_top_k".
  • arguments: the arguments for the command at the same index.

The two commands are:

  • "log_message" with arguments [timestamp, message]: record that message occurred at timestamp. The result for this command is None.
  • "get_top_k" with arguments [start_time, end_time, k]: consider only recorded events whose timestamp t satisfies start_time <= t <= end_time (both ends inclusive). Count how many times each distinct message appears among them, then return a list of the k best messages as described below.

Any other operation name should raise a ValueError.

Ranking Rules

  • A message with a higher count comes before one with a lower count.
  • If two messages have the same count, the lexicographically smaller message comes first (standard Python string comparison).
  • If the window contains fewer than k distinct messages, return all of them in the same order.
  • If the window contains no events, return an empty list.

Queries only see events logged by earlier commands in the same batch.

Output

Return a list with one entry per command: None for each "log_message", and the ranked list of strings for each "get_top_k".

Constraints

  • 1 <= timestamp <= 10^9
  • Timestamps across "log_message" commands are non-decreasing in the order they appear.
  • start_time <= end_time
  • 1 <= k <= 100
  • 1 <= len(message) <= 50
  • At most 10^5 "log_message" commands.
  • At most 500 "get_top_k" commands.

Examples

Example 1

Input: rank_window_events(['log_message', 'log_message', 'log_message', 'log_message', 'log_message', 'log_message', 'get_top_k', 'get_top_k'], [[1, 'error'], [2, 'info'], [3, 'error'], [5, 'warn'], [6, 'info'], [7, 'error'], [1, 5, 2], [4, 7, 3]])

Output: [None, None, None, None, None, None, ['error', 'info'], ['error', 'info', 'warn']]

Example 2

Input: rank_window_events(['get_top_k', 'log_message', 'get_top_k'], [[1, 10, 1], [5, 'only'], [1, 10, 3]])

Output: [[], None, ['only']]

Hints

Hint 1

Because log timestamps never decrease, the events inside any query window form one contiguous block of the log. Think about how to find its two ends without scanning everything.

Hint 2

Use binary search twice: bisect_left for the first timestamp >= start_time, and bisect_right for one past the last timestamp <= end_time. Duplicate timestamps are handled correctly by this left/right pairing.

Hint 3

Count the messages in that block with a hash map, then sort the distinct messages by the key (-count, message) and cut the result to the first k.