All problems
Undo Stack with Transactions
Problem
Simulate a key-value store with nested transactions and rollback.
operations contains "set" (keys[i] = values[i]), "begin", "commit" (merge the transaction into its parent), "rollback" (undo the current transaction and record how many sets were reverted), or "get" (value or None). A rollback without an active transaction records 0.
Return the list of recorded results (None for set, begin, and commit).
Hint
Each transaction logs the set operations so rollback can restore previous values.
Test cases
case 1
in: [["set","begin","set","rollback","get"],["a","","a","","a"],[1,0,2,0,0]]
expected: [null,null,null,1,1]
case 2
in: [["begin","set","commit","rollback","get"],["","a","","","a"],[0,5,0,0,0]]
expected: [null,null,null,0,5]
case 3
in: [["rollback"],[""],[0]]
expected: [0]
case 4
in: [["begin","begin","set","rollback","get"],["","","a","","a"],[0,0,9,0,0]]
expected: [null,null,null,1,null]
Starter codePython
def undo_transactions(operations, keys, values):
# Your code here
pass4 test cases · run your code against them in the editor