KatInterview

Cache, Quota Gate and Burst Counter: Batch Operation Runner

Data Structure DesignAuto-graded · 21 test cases
HardAuto-gradedData Structure Designlfu-cacheordered-dictrate-limiting

Last updated

Simulate a batch of operations on an O(1) LFU cache, a fixed-window per-user quota check, and a two-pointer peak request counter.

Cache, Quota Gate and Burst Counter

You are building the core of a small service that bundles three independent features. You are given the configuration of the service and a list of operations to execute in order. Return the result of each operation.

Features

1. LFU cache with at most capacity entries, mapping integer keys to integer values.

  • get_cache(key): if the key is present, return its value and count this as a use of the key. If it is absent, return -1 and change nothing.
  • put_cache(key, value): if the key already exists, overwrite its value and count this as a use. Do not evict anything in this case. If the key is new and the cache is already full, first evict the entry with the smallest use count. If several entries share that smallest count, evict the one that was used least recently. Then insert the new key with a use count of 1.
  • A newly inserted key has a use count of 1. Every successful get_cache and every put_cache on an existing key increases the count by 1.
  • If capacity is 0, put_cache does nothing.
  • Both cache operations are expected to run in O(1) time.

2. Fixed-window quota gate

  • is_allowed(user_id, current_time): time is divided into consecutive windows of length window_size. The window of a request is current_time // window_size.
  • Each user may have at most limit accepted requests in each window. Counts are tracked per user and per window independently.
  • If the user's accepted count in that window is below limit, accept the request (return true) and increase the count. Otherwise return false. Rejected requests do not consume quota.

3. Peak burst counter

  • max_throughput(timestamps): timestamps is a list of request times sorted in non-decreasing order. Find the largest number of requests that fit inside one window of length window_size, where a group of requests fits if latest - earliest <= window_size (both ends inclusive). Duplicate timestamps each count. An empty list gives 0.

The cache and the quota gate keep their state across all operations in the call. max_throughput depends only on the list passed to it.

Input

  • capacity, limit, window_size: the configuration described above. window_size is used by both the quota gate and the burst counter.
  • operations: a list of strings, each one of "get_cache", "put_cache", "is_allowed", "max_throughput".
  • arguments: a list with the same length as operations. The entry at index i holds the arguments for operations[i]:
    • get_cache: [key]
    • put_cache: [key, value]
    • is_allowed: [user_id, current_time]
    • max_throughput: [timestamps]

Output

A list with one entry per operation, in order:

  • get_cache: the value, or -1 on a miss.
  • put_cache: None.
  • is_allowed: a boolean.
  • max_throughput: an integer.

Constraints

  • capacity, limit and window_size are non-negative integers up to 10^5. window_size is at least 1.
  • len(operations) == len(arguments).
  • Every operation name is one of the four listed above.
  • Each timestamps list is sorted in non-decreasing order.
  • Keys, values, user ids and times are integers.

Examples

Example 1

Input: process_service_ops(2, 2, 250, ['put_cache', 'put_cache', 'get_cache', 'put_cache', 'get_cache', 'max_throughput'], [[1, 1], [2, 2], [1], [3, 3], [2], [[100, 200, 300, 500, 600]]])

Output: [None, None, 1, None, -1, 3]

Example 2

Input: process_service_ops(1, 1, 10, ['is_allowed', 'is_allowed', 'is_allowed', 'is_allowed'], [[7, 0], [7, 1], [7, 10], [8, 1]])

Output: [True, False, True, True]

Example 3

Input: process_service_ops(2, 2, 100, ['put_cache', 'put_cache', 'get_cache', 'put_cache', 'get_cache', 'get_cache'], [[1, 10], [2, 20], [1], [3, 30], [2], [3]])

Output: [None, None, 10, None, -1, 30]

Hints

Hint 1

For the cache, group keys by use count. Within each group keep keys in order of last use, for example with an OrderedDict per count, and track the smallest count currently present.

Hint 2

When a key's count goes from f to f+1, remove it from group f and append it to group f+1. If group f was the minimum and is now empty, the minimum becomes f+1. A fresh insert always resets the minimum to 1.

Hint 3

For the quota gate, key a counter by (user_id, current_time // window_size). For the burst counter, slide a left pointer forward while timestamps[right] - timestamps[left] > window_size.