KatInterview

Rolling Read/Write Rate Tracker for an In-Memory Dictionary Store

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designdesignsliding-windowdeque

Last updated

Simulate a key-value store that logs reads and writes with timestamps and answers per-second average rate queries over trailing windows, using compact time buckets.

Rolling Read/Write Rate Tracker

You are building a small in-memory key-value store that also measures how busy it is. Besides storing and fetching values, it must report the average number of reads and writes per second over a trailing time window.

Because the store is driven by a single function call, you are given a list of operation names and a parallel list of argument lists. Process them in order, as if calling methods on one fresh store, and return one result per operation.

Operations

Each operations[i] is one of the following, with its parameters in arguments[i]:

  • "put" with [key, value, timestamp]: stores value under key, replacing any existing value, and records one write event at timestamp. Result: None.
  • "get" with [key, timestamp]: records one read event at timestamp, then returns the value stored for key, or None if the key does not exist. The read event is recorded even when the key is missing.
  • "average_gets" with [window_seconds, timestamp]: returns the number of read events whose time lies in the inclusive range [timestamp - window_seconds + 1, timestamp], divided by window_seconds.
  • "average_puts" with [window_seconds, timestamp]: same as above, but for write events.

Any other operation name must raise ValueError.

Rules

  • Timestamps are integer seconds and never decrease from one operation to the next (across all operation types).
  • The divisor is always exactly window_seconds, regardless of how much history exists. Seconds before the first event, or before time 0, simply contain no events but still count in the divisor.
  • Several events may share the same timestamp; all of them count.
  • Overwriting a key still counts as a write. Reading a missing key still counts as a read.
  • A window with no matching events yields 0.0 (a numeric zero).
  • Memory should grow with the number of distinct seconds that had activity, not with the raw number of operations.

Function

Implement metrics_kv_store(operations, arguments) returning the list of results, one per operation, in order.

Constraints

  • 1 <= len(key), len(value) <= 100
  • 1 <= timestamp <= 2 * 10^9
  • 1 <= window_seconds <= 604800
  • At most 2 * 10^5 operations in total.
  • Timestamps are non-decreasing across the whole sequence.

Examples

Example 1

Input: metrics_kv_store(['put', 'put', 'get', 'average_gets', 'average_puts', 'average_puts'], [['a', '1', 10], ['b', '2', 10], ['a', 15], [10, 15], [10, 15], [5, 16]])

Output: [None, None, '1', 0.1, 0.2, 0]

Hints

Hint 1

Treat the storage and the statistics as two independent pieces: a dictionary for the data, and one event log per kind of operation (reads and writes).

Hint 2

Since time never goes backwards, events at the same second can be merged into a single [second, count] bucket at the end of a deque. This keeps memory tied to distinct active seconds.

Hint 3

No query reaches back more than 604800 seconds, so buckets older than timestamp - 604800 + 1 can be dropped from the front. To answer a query, add up the counts of buckets inside the window and divide by window_seconds, not by the number of active seconds.