KatInterview

Session Key Manager with Deferred Cleanup

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designheaphash-maplazy-deletion

Last updated

Tests building a session-key store with TTL-based lazy eviction using a hash map and min-heap to process a sequence of operations.

You are simulating a session-key store where each key has a time-to-live (TTL). A key is considered expired once the current time is greater than or equal to its expiration timestamp. You process a list of operations sequentially, each paired with a simulated current time.

Implement a function that takes three parallel lists — operations, arguments, and current_times — and returns a list containing the result of each operation.

Operations

  • "upsert": arguments[i] is [key, ttl]. Register or overwrite the key so that its expiration timestamp equals current_times[i] + ttl. Always pushes a result of None (rendered as null).
  • "get": arguments[i] is [key]. If the key exists in the store and its expiration timestamp is strictly greater than the current time, return the expiration timestamp. Otherwise return -1. If the key exists but has expired, remove it from the store before returning -1 (lazy eviction).
  • "count_active": arguments[i] is []. Return the number of keys currently in the store that have not expired. Before counting, purge every key whose expiration timestamp is less than or equal to the current time.

Expiration boundary rule

A key with expiration timestamp E observed at time T is expired when T >= E. It is still active when T < E. Apply this rule consistently in both "get" and "count_active".

Stale-heap handling

When "upsert" overwrites an existing key, the old heap entry must not corrupt the store. After popping any heap entry (expiration, key), only treat it as the live entry if the store still maps key to that exact expiration value. Otherwise discard the entry and continue.

Constraints

  • 1 <= len(operations) <= 10^5
  • operations[i] is one of "upsert", "get", "count_active".
  • key is a non-empty string of lowercase letters and underscores.
  • ttl is a positive integer.
  • current_times is non-decreasing across the operation list (time never moves backward).
  • The total number of distinct keys across all operations is at most 10^5.

Examples

Example 1

Input: session_key_ops(['upsert', 'upsert', 'get', 'get', 'count_active'], [['user_a', 10], ['user_b', 5], ['user_b'], ['user_b'], []], [1000, 1000, 1000, 1007, 1007])

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

Example 2

Input: session_key_ops(['upsert', 'upsert', 'upsert', 'get', 'get', 'count_active'], [['a', 10], ['b', 5], ['a', 20], ['a'], ['b'], []], [100, 100, 105, 105, 105, 105])

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

Example 3

Input: session_key_ops(['upsert', 'get', 'count_active', 'get', 'count_active'], [['x', 3], ['x'], [], ['x'], []], [10, 13, 13, 14, 14])

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

Hints

Hint 1

Use a hash map to store each key's current expiration timestamp for O(1) lookups and updates.

Hint 2

Push every upsert onto a min-heap keyed by expiration; stale entries are filtered out by checking the map when popped.

Hint 3

For count_active, pop all heap entries whose expiration is at or before the current time, deleting matching keys from the map, then return the map size.