KatInterview

Design an In-Memory Key-Value Engine with Transactions

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designhash-tablestackdesign

Last updated

Implement an in-memory key-value database supporting frequency counting and nested transaction rollbacks.

Implement an in-memory key-value database engine that processes a sequence of instructions line by line and returns its output.

The input is provided as a list of command strings commands. The engine supports seven instructions:

  • SET key value: Sets key to value. Updates existing assignments or creates a new entry if key was not present. Produces no output.
  • GET key: Looks up key. Appends value to the output list if found, or "NULL" if key does not exist.
  • DELETE key: Removes key and its mapped value from the database if present. If key does not exist, does nothing. Produces no output in either case.
  • COUNT value: Appends the count of keys currently assigned to value (as a decimal string, e.g., "0", "1", "2") to the output list.
  • BEGIN: Opens a new nested transaction block. Produces no output.
  • ROLLBACK: Reverts all state modifications made during the most recently opened active transaction. Appends "NO TRANSACTION" to the output list if no transaction is currently open.
  • COMMIT: Makes all modifications across all open transaction blocks permanent and closes all active transactions. Produces no output, even if no transaction is open.

Rules & Details

  1. Mutation Logging: SET and DELETE operations append state changes to an internal change log regardless of whether a transaction is open. This includes executing DELETE on non-existent keys (which logs that the key did not exist before).
  2. Rollback Mechanics: A successful ROLLBACK pops the innermost transaction scope and undoes all log entries recorded since that scope was opened, applied in reverse chronological (LIFO) order.
  3. Commit Mechanics: A COMMIT closes all active transaction blocks simultaneously and purges all logged history. Subsequent ROLLBACK commands will output "NO TRANSACTION" unless a new transaction is subsequently opened with BEGIN.

Constraints

  • $1 \le \text{len}(commands) \le 100{,}000$
  • Keys and values consist of ASCII characters excluding whitespace.
  • SET, GET, DELETE, COUNT, and BEGIN should operate in $O(1)$ expected time.
  • ROLLBACK should operate in $O(R)$ time, where $R$ is the number of changes logged in the reverted transaction block.

Output Format

Return a list of strings containing all output values emitted by GET, COUNT, and failed ROLLBACK commands, in the exact sequence they were generated.

Examples

Example 1

Input: execute_commands(['SET a 10', 'GET a', 'COUNT 10', 'DELETE a', 'GET a', 'COUNT 10'])

Output: ['10', '1', 'NULL', '0']

Example 2

Input: execute_commands(['SET a 10', 'BEGIN', 'SET a 20', 'GET a', 'ROLLBACK', 'GET a', 'COUNT 10'])

Output: ['20', '10', '1']

Example 3

Input: execute_commands(['SET a 10', 'BEGIN', 'SET a 20', 'BEGIN', 'SET b 20', 'COUNT 20', 'ROLLBACK', 'GET b', 'GET a', 'COMMIT', 'ROLLBACK'])

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

Example 4

Input: execute_commands(['GET missing', 'ROLLBACK', 'COUNT absent', 'DELETE missing', 'SET x same', 'SET y same', 'COUNT same'])

Output: ['NULL', 'NO TRANSACTION', '0', '2']

Hints

Hint 1

Use two hash maps: one mapping key -> value for direct lookup, and another mapping value -> count to serve COUNT queries in O(1) time.

Hint 2

Instead of copying entire hash maps when BEGIN is executed, append write operations (SET and DELETE) to an undo stack recording (key, existed_flag, old_value).

Hint 3

Track transaction scopes by pushing the current height of the undo stack onto a transaction stack during BEGIN.