Design a nested transaction store
Company: Applied Intuition
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates understanding of transactional state management, nested transaction semantics, scope resolution, and data-structure design for an in-memory key–value store.
Constraints
- 0 <= len(commands) <= 10^4
- Keys are non-empty strings of length at most 20
- Values are integers in the range [-10^9, 10^9]
- Transactions may be nested arbitrarily within the command list
Examples
Input: [('SET', 'X', 20), ('BEGIN',), ('BEGIN',), ('SET', 'X', 10), ('RETURN', 'X'), ('APPLY',), ('DISCARD',), ('RETURN', 'X')]
Expected Output: [10, 20]
Explanation: The inner transaction changes X to 10, so the first RETURN sees 10. APPLY merges that change into its parent transaction. DISCARD then rolls back the parent transaction, so the base value 20 is visible again.
Input: [('RETURN', 'A'), ('APPLY',), ('DISCARD',)]
Expected Output: [None, 'NO TRANSACTION', 'NO TRANSACTION']
Explanation: A was never set, so RETURN gives None. There is no active transaction for APPLY or DISCARD, so both produce 'NO TRANSACTION'.
Hints
- Think of each BEGIN as pushing a new write layer onto a stack.
- For RETURN, check the newest transaction first and work outward until you find the key.