KatInterview

Nested Transaction Log for an In-Memory Dictionary

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designhash-mapstacktransactions

Last updated

Simulate a key-value store with nested begin, commit and rollback by replaying a command list and recording each read result.

You are given a script of commands for a small in-memory key-value store that supports nested transactions. Replay the script from an empty store and report what each command produces.

Commands

Command i is commands[i], and its arguments are in params[i].

CommandArgumentsEffect
"set"[key, value]Store value under key, replacing any existing value.
"get"[key]Read the current value of key.
"delete"[key]Remove key. Deleting a key that does not exist is allowed and changes nothing.
"begin"[]Open a new transaction, nested inside any transaction that is already open.
"commit"[]Close the innermost open transaction and keep its changes.
"rollback"[]Close the innermost open transaction and undo every change made since its matching begin.

Transaction rules

  • Transactions form a stack. commit and rollback act only on the innermost open transaction, never on all of them.
  • Reads inside a transaction see all writes made so far, including writes from enclosing transactions and uncommitted writes.
  • rollback restores each affected key to exactly the state it had when the innermost transaction began. A key that was overwritten gets its old value back. A key that was deleted is restored. A key that did not exist before the transaction is absent again.
  • Committing a nested transaction does not make its changes permanent. They are handed to the enclosing transaction, so rolling back the enclosing transaction also undoes them.
  • Committing a top-level transaction makes its changes permanent.
  • If no transaction is open, commit and rollback do nothing.

Output

Return a list with one entry per command, in order.

  • For "get", the entry is the key's current value, or None if the key is missing or deleted.
  • For every other command, the entry is None.

Constraints

  • len(commands) == len(params).
  • Every command is one of "set", "get", "delete", "begin", "commit", "rollback", and its argument list has the shape shown above.
  • Keys are strings and values are integers.
  • get, set, delete and begin should run in expected constant time. commit and rollback may take time proportional to the number of distinct keys changed in the innermost transaction.
  • The number of commands is small enough for a single linear pass.

Examples

Example 1

Input: simulate_store(['set', 'begin', 'set', 'delete', 'get', 'rollback', 'get'], [['a', 1], [], ['a', 2], ['b'], ['a'], [], ['a']])

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

Hints

Hint 1

Keep one dictionary for the live data, and a stack of per-transaction undo logs, one dictionary per open transaction.

Hint 2

The first time a transaction touches a key, record what the key held before (or that it was absent). Later writes to the same key in that transaction should not overwrite this record.

Hint 3

On rollback, replay the innermost log: put back old values and remove keys that were absent. On a nested commit, move to the parent log only the entries for keys the parent has not recorded yet, since the parent needs the older state.