KatInterview

Sliding-Window Leaderboard Replay Simulator

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

Last updated

Simulate a class that logs timestamped events and reports the K most frequent IDs in a closed time window, with out-of-order events and queries.

Sliding-Window Leaderboard Replay Simulator

A leaderboard service counts how often string IDs appear in a stream of timestamped events. At any moment it can report which IDs were most frequent in a recent time window.

You are given a script of method calls on this service. Write a function that replays the script and returns the result of every call.

The service

The service supports three operations, identified by these exact names:

  • "TopTracker" takes [window_size_ms, k]. It sets up the service with a window length (in milliseconds) and the number of IDs to report. It discards all previously stored events and replaces the old settings.
  • "record_event" takes [entity_id, timestamp_ms]. It stores one occurrence of entity_id at time timestamp_ms.
  • "get_top_k" takes [current_timestamp_ms]. It returns a list of at most k IDs ranked as described below.

Ranking rules

For a get_top_k call with query time t, the window is the closed interval [t - window_size_ms, t]. An event counts if its timestamp lies in this interval, endpoints included.

  • An ID's frequency is the number of stored events for that ID inside the window. Several events with the same ID and the same timestamp each count separately.
  • IDs with frequency 0 are never reported.
  • Sort by frequency, highest first. If frequencies are equal, the lexicographically smaller ID comes first.
  • Return the first k IDs of that order. If fewer than k IDs qualify, return all of them. If none qualify, return [].

Events can be recorded in any timestamp order, and queries can be made in any time order. A query never changes the stored data. An event with a timestamp later than t is ignored by that query, but it stays stored for later queries.

Function

operations and arguments have the same length, and arguments[i] holds the argument list for operations[i]. Return a list of the same length. Each entry is None for "TopTracker" and "record_event", and a list of ID strings for "get_top_k".

Constraints

  • operations and arguments have equal length, and every operation name is one of the three above.
  • "TopTracker" is the first operation in every script, though it may appear again later to reset the service.
  • window_size_ms >= 0 and k >= 1.
  • Timestamps are integers and may arrive in any order.
  • IDs are non-empty strings, compared by ordinary string ordering.
  • A get_top_k call should not rescan every stored event. Aim to be faster than a full scan of the history.

Examples

Example 1

Input: replay_window_leaderboard(['TopTracker', 'record_event', 'record_event', 'record_event', 'get_top_k'], [[1000, 2], ['A', 1000], ['B', 1200], ['A', 1500], [1800]])

Output: [None, None, None, None, ['A', 'B']]

Example 2

Input: replay_window_leaderboard(['TopTracker', 'get_top_k', 'record_event', 'record_event', 'get_top_k'], [[100, 3], [1000], ['x', 900], ['y', 899], [1000]])

Output: [None, [], None, None, ['x']]

Example 3

Input: replay_window_leaderboard(['TopTracker', 'record_event', 'record_event', 'record_event', 'record_event', 'get_top_k'], [[10, 3], ['b', 0], ['a', 0], ['c', 0], ['a', 10], [10]])

Output: [None, None, None, None, None, ['a', 'b', 'c']]

Hints

Hint 1

Think about whether you can ever discard an event. If query times can go backward, an event that is outside one window may be needed by a later query.

Hint 2

Store, for each ID, a list of its timestamps kept in sorted order. The number of events in a closed range is then the difference of two binary-search positions.

Hint 3

Use bisect_left for the lower end and bisect_right for the upper end so both endpoints are included. Rank the IDs with the key (-count, id) so ties go to the smaller ID.