KatInterview

In-Memory Store with Nested Transaction Support

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

Last updated

Tests your ability to implement layered state management with nested scopes, tombstone deletion tracking, and atomic commit/rollback semantics.

In-Memory Store with Nested Transaction Support

Implement a function that simulates an in-memory key-value store supporting nested transactions. The store must handle reads, writes, deletions, and transaction boundaries (begin, commit, rollback) while maintaining correct visibility across transaction layers.

You will receive two parallel lists:

  • operations: a list of operation names ("put", "get", "delete", "begin", "commit", "rollback")
  • arguments: a list where arguments[i] contains the parameters for operations[i]

Your function must process each operation in sequence and return a list of results. Most operations return None, but get returns the current value for a key (or None if the key doesn't exist or has been deleted).

Operation Specifications

put

  • Arguments: [key, value]
  • Behavior: Store value under key. If a transaction is active, the change is scoped to the current transaction layer.
  • Returns: None

get

  • Arguments: [key]
  • Behavior: Retrieve the value for key. The lookup must respect transaction scope: check the innermost active transaction first, then parent transactions, finally the base store.
  • Returns: The associated value, or None if the key is absent or deleted in the visible scope.

delete

  • Arguments: [key]
  • Behavior: Mark key as deleted. Within a transaction, this hides the key from get operations but does not affect parent scopes until committed.
  • Returns: None

begin

  • Arguments: []
  • Behavior: Open a new transaction scope. Changes made after this point are isolated until committed or rolled back.
  • Returns: None

commit

  • Arguments: []
  • Behavior: Merge all changes from the current transaction scope into its parent scope (or into the base store if no parent exists). If no transaction is active, this is a no-op.
  • Returns: None

rollback

  • Arguments: []
  • Behavior: Discard all changes made in the current transaction scope, restoring visibility to the parent scope. If no transaction is active, this is a no-op.
  • Returns: None

Constraints

  • 1 <= len(operations) <= 10^4
  • Keys and values can be any hashable type (typically strings or integers)
  • Transaction nesting depth will not exceed 100 levels
  • Operations will only be the six types listed above
  • A get on a deleted key must return None, not an error
  • commit and rollback with no active transaction must be handled gracefully (no-op behavior)

Examples

Example 1

Input: transactional_kv_store(['put', 'begin', 'put', 'get', 'rollback', 'get'], [['a', 1], [], ['a', 2], ['a'], [], ['a']])

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

Example 2

Input: transactional_kv_store(['put', 'begin', 'put', 'begin', 'delete', 'get', 'commit', 'get', 'commit', 'get'], [['x', 1], [], ['x', 2], [], ['x'], ['x'], [], ['x'], [], ['x']])

Output: [None, None, None, None, None, None, None, None, None, None]

Hints

Hint 1

Think of each transaction as a layer of changes stacked on top of the base store. Reads should search from the top layer downward.

Hint 2

How can you represent a deletion within a transaction layer without actually removing the key from parent layers? Consider using a sentinel value that acts as a marker.

Hint 3

When committing, you only need to merge one layer upward. Use dictionary update to efficiently transfer all changes from the child scope into its immediate parent.