KatInterview

In-Memory Store with Transaction Layering

Data Structure DesignAuto-graded · 20 test cases
EasyAuto-gradedData Structure Designdesign-dsstacks-queuesarrays

Last updated

Implement an in-memory key-value database supporting GET, SET, DELETE, and nested transaction semantics (BEGIN, ROLLBACK, COMMIT).

Design and implement a system that simulates an in-memory key-value database processing a list of commands. The database supports data mutation, point-in-time reads, and nested transaction contexts.

Command Specifications

Commands are provided as single space-separated string instructions.

Data Commands:

  • SET <key> <value>: Sets the value of <key> to <value>.
  • GET <key>: Reads the current value of <key>. If the key is not currently present in the database, it produces the string "NULL".
  • DELETE <key>: Removes <key> from the database. If <key> does not exist, this command has no effect.

Transaction Commands:

  • BEGIN: Opens a new transaction frame. Transactions can be nested arbitrarily deep.
  • ROLLBACK: Reverts all mutations made within the innermost open transaction frame and closes that frame. If no transaction frame is currently open, it produces the string "NO TRANSACTION".
  • COMMIT: Permanently applies all mutations across all currently open transaction frames and closes all open frames. If no transaction frame is currently open, it produces the string "NO TRANSACTION".

Rules & State Behavior

  1. Operations take effect immediately; a GET command must reflect all previous non-rolled-back data commands (including modifications within any active transaction block).
  2. Deleting a key that does not exist inside a transaction must still record its absent state for that transaction layer (so rolling back will restore it to absent, rather than leaving later modifications).
  3. COMMIT or ROLLBACK when no transactions are open must append "NO TRANSACTION" to the output and alter no data state.
  4. The output list must contain the strings generated in order by all GET commands and any "NO TRANSACTION" messages triggered by invalid ROLLBACK or COMMIT instructions.

Constraints

  • $1 \le \text{len(commands)} \le 10^5$
  • Keys and values are non-empty strings without whitespace (up to 32 characters in length).
  • Transaction nesting depth can be up to $10^4$.
  • Data queries (GET) and mutations (SET) should execute in $O(1)$ expected time. Copying the full state of the database on BEGIN is not allowed.

Examples

Example 1

Input: process(['SET a 10', 'GET a', 'BEGIN', 'SET a 20', 'GET a', 'BEGIN', 'DELETE a', 'GET a', 'ROLLBACK', 'GET a', 'COMMIT', 'GET a', 'ROLLBACK'])

Output: ['10', '20', 'NULL', '20', '20', 'NO TRANSACTION']

Example 2

Input: process(['GET missing', 'BEGIN', 'SET x one', 'BEGIN', 'SET x two', 'COMMIT', 'GET x', 'ROLLBACK'])

Output: ['NULL', 'two', 'NO TRANSACTION']

Hints

Hint 1

Maintaining layered dictionary masks forces GET operations to traverse up to transaction_depth dictionaries, which exceeds time limits.

Hint 2

Instead of overlaying frames for reads, maintain a single live dictionary and log undo operations in a stack of dictionaries (one per open transaction frame).

Hint 3

In an active transaction frame, only record a key's state the first time it is modified within that frame. Use a unique sentinel object to represent keys that were previously missing.