KatInterview

Nested Undo-Log Session Store with Value Tallies

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-mapundo-logtransactions

Last updated

Simulate an in-memory key-value store with nested transactions, a global commit, and a live per-value key tally, processing a batch of operations and returning each result.

Nested Undo-Log Session Store with Value Tallies

You are simulating a small in-memory database that maps string keys to scalar values and supports nested transactions. You are given a batch of commands. Process them in order and report one result per command.

Input

  • operations: a list of command names. Each is one of "set", "get", "count", "begin", "commit", "rollback".
  • arguments: a list of the same length. arguments[i] is the list of arguments for operations[i].

Commands

CommandArgumentsEffectResult
set[key, value]Map key to value, replacing any previous value.None
get[key]Look up the value currently visible for key.The value, or None if the key does not exist.
count[value]Count the keys whose currently visible value equals value.The integer count (0 if none).
begin[]Open a new transaction, nested inside any transaction already open.None
commit[]Make permanent the changes of all currently open transactions and close all of them.None
rollback[]Undo every change made since the most recent still-open begin, and close only that transaction.None

Rules

  • The visible state always includes uncommitted changes made in open transactions. get and count must reflect it.
  • Rolling back a transaction restores exactly the state that existed when that transaction began. A key created inside the transaction must be removed again, not left mapped to None.
  • Rolling back an inner transaction leaves the changes of the enclosing transactions in place. Those can then be rolled back separately.
  • If commit or rollback is called while no transaction is open, it does nothing and still returns None.
  • After a commit, no transactions remain open, so a following rollback does nothing.
  • A value may itself be None. A key mapped to None exists, and count(None) counts such keys. get returns None both for a missing key and for a key mapped to None.
  • Setting a key to the value it already holds changes no counts.

Output

Return a list with one entry per command, in order, holding the result described in the table.

Constraints

  • operations and arguments have the same length, between 0 and about 10^5.
  • Keys are strings. Values are hashable scalars (integers, strings, None, and so on).
  • Every command comes with the argument list described above.
  • The expected running time is close to linear in the number of commands.

Examples

Example 1

Input: run_session_store(['set', 'get', 'count', 'begin', 'set', 'get', 'count', 'rollback', 'get', 'count'], [['a', 10], ['a'], [10], [], ['a', 20], ['a'], [20], [], ['a'], [20]])

Output: [None, 10, 1, None, None, 20, 1, None, 10, 0]

Example 2

Input: run_session_store(['set', 'set', 'begin', 'set', 'begin', 'set', 'count', 'rollback', 'get', 'commit', 'count', 'rollback'], [['a', 1], ['b', 1], [], ['a', 2], [], ['b', 2], [2], [], ['b'], [], [1], []])

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

Example 3

Input: run_session_store(['get', 'count', 'commit', 'rollback', 'begin', 'set', 'set', 'count', 'rollback', 'get', 'count'], [['missing'], [None], [], [], [], ['x', None], ['y', None], [None], [], ['x'], [None]])

Output: [None, 0, None, None, None, None, None, 2, None, None, 0]

Hints

Hint 1

Keep one live dictionary that always holds the currently visible state, so get is a plain lookup. Maintain a second dictionary from value to number of keys and update it on every write.

Hint 2

For each open transaction, keep an undo record that maps a key to the value it had before that transaction first modified it. Use a unique sentinel for 'did not exist' so it differs from a real None value.

Hint 3

Record only the first change to a key per transaction. Rollback pops the top record and restores each key, adjusting the value tallies. Commit just discards all undo records.