KatInterview

Simulate a Time-Aware Key-Value Store with Transactions

Data Structure DesignAuto-graded · 21 test cases
HardAuto-gradedData Structure Designhash-maptransactionssimulation

Last updated

Tests implementation of an in-memory store combining TTL expiration, a logical clock, and atomic single-transaction semantics with rollback.

Problem

Build a function that simulates an in-memory key-value store driven by a sequence of commands. The store tracks a logical clock and supports expiring keys, plus a single-level transaction that can be committed or discarded.

Commands

CommandArgumentsReturnsDescription
SETkey, value, ttlnothingStores value under key. The entry expires at current_time + ttl (exclusive).
GETkeyvalue or NoneReturns the stored value if the key exists and has not expired; otherwise None.
DELETEkeynothingRemoves the key. No-op if the key is absent.
BEGIN—nothingOpens a transaction. All subsequent writes are staged and not yet visible outside the transaction.
COMMIT—nothingFlushes all staged writes into the committed store and closes the transaction.
ROLLBACK—nothingDiscards all staged writes and closes the transaction, leaving the committed store unchanged.
TICK—nothingAdvances the logical clock by 1.

Expiration rule

A key set at time t with a TTL of d expires at t + d. It is visible only while current_time < t + d. At the moment current_time == t + d the key is already gone.

Transaction semantics

  • At most one transaction is active at any moment.
  • While a transaction is open, GET reads staged writes first; if the key is not staged it falls back to the committed store.
  • A DELETE issued inside a transaction hides the key from subsequent GET calls within that same transaction, even if the key exists in the committed store.
  • COMMIT makes all staged changes permanent. ROLLBACK abandons them.
  • TICK always advances the clock, even inside a transaction. Time is never rolled back.

Your task

Implement the function below. It receives two parallel lists:

  • commands — a list of command strings, each one of the seven names above.
  • args — a list of argument lists. Commands that take no arguments have an empty list []. SET has three elements; GET and DELETE have one; the rest have none.

Return a list of results, one per command. Commands that produce no output contribute None to the result list. GET contributes either the value string or None.

Constraints

  • 1 ≤ len(commands) ≤ 1000
  • All keys and values are non-empty strings.
  • ttl is a positive integer.
  • The logical clock starts at 0 before any command runs.
  • BEGIN is never called while a transaction is already open.
  • COMMIT and ROLLBACK are always called with an active transaction.

Examples

Example 1

Input: run_expiring_store(['SET', 'TICK', 'GET', 'BEGIN', 'SET', 'GET', 'ROLLBACK', 'GET'], [['a', '1', 10], [], ['a'], [], ['a', '2', 5], ['a'], [], ['a']])

Output: [None, None, '1', None, None, '2', None, '1']

Hints

Hint 1

Track absolute expiry times (time_of_set + ttl) rather than counting down a remaining TTL — then an expiry check is a single comparison at read time regardless of how many ticks have passed.

Hint 2

A DELETE inside a transaction cannot simply remove the key from the staging area, because the key may still exist in the committed store and would reappear on the next GET. Use a sentinel value (e.g. None) in the staging dict to mean 'deleted in this transaction'.

Hint 3

On COMMIT, iterate the staging dict and apply each entry to the committed store: a sentinel becomes a deletion, anything else becomes an upsert. On ROLLBACK, just throw the staging dict away — the committed store was never touched.