KatInterview

Count Lower Readings in a Trailing Time Window

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designsliding-windowbinary-searchsimulation

Last updated

Simulate a sequence of record and query operations over a timestamped stream, counting readings in a trailing window that are strictly lower than the latest reading.

Spike Counter over a Metric Stream

You are given a log of operations performed on a metric monitor. The monitor receives numeric readings tagged with integer timestamps (in seconds) and can be asked how many recent readings are lower than the newest one.

Implement a function that replays the operations in order and returns one output per operation.

Operations

You receive two parallel lists, operations and arguments. The i-th entry of arguments holds the arguments for the i-th entry of operations.

  • "record" with arguments [timestamp, value]: store a reading with the given integer timestamp and integer value. The output for this operation is None.
  • "get_spike_count" with arguments [window_size]: the output is the number of stored readings that satisfy both of the following:
    • The reading lies inside the trailing window of window_size seconds.
    • Its value is strictly less than the value of the most recently recorded reading.

Precise Rules

  • The "current" reading is the reading recorded most recently (the last record performed so far), not a separate query time.
  • Let t be the timestamp of the current reading. The trailing window is the inclusive range of integer seconds [t - window_size + 1, t]. A reading is inside the window if its timestamp is in this range.
  • The current reading is never counted, since its value is not strictly less than itself. Other readings with the same timestamp as the current one are counted if they are inside the window and have a smaller value.
  • Readings with a value equal to the current value are not counted.
  • If nothing has been recorded yet, the answer to a query is 0.
  • If window_size is zero or negative, the window contains no readings and the answer is 0.
  • A window that reaches back before the earliest reading simply includes all readings up to that point.
  • Queries do not modify any stored data. Later queries use whatever has been recorded up to that moment.

Output

Return a list with the same length as operations. Entries for "record" are None; entries for "get_spike_count" are integers.

Constraints

  • len(operations) == len(arguments).
  • Timestamps in record operations are integers and arrive in non-decreasing order.
  • Values and window_size are integers.
  • Every operation name is either "record" or "get_spike_count".

Examples

Example 1

Input: count_lower_readings(['record', 'record', 'record', 'record', 'record', 'get_spike_count'], [[1, 10], [2, 5], [3, 12], [4, 8], [5, 15], [3]])

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

Hints

Hint 1

Because timestamps never decrease, the readings inside any trailing window always form a contiguous block at the end of the history.

Hint 2

Work out the earliest timestamp that is still inside the window as `current_timestamp - window_size + 1`, then count only readings from that point on whose value is below the latest value.

Hint 3

To skip older readings quickly, keep timestamps in a list and use binary search (`bisect_left`) to find where the window begins. Do not discard old readings, because later queries may use a larger window.