FunctionRacerProblemsRace

Store With Nested Transactions

MediumClassesPython, JavaScript, TypeScript, Java, C++

Implement a class TxStore, a store of integer values under string keys whose changes can be grouped into transactions, and transactions can be nested. Write these methods:

The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

TxStore()
put("a", 1)
begin() returns 1
put("a", 2)
put("b", 2)
begin() returns 2
put("c", 3)
commit() returns 1
read("c") returns 3
rollback() returns 3
read("a") returns 1
read("b") returns -1

The inner transaction is committed, so its change to c joins the outer one. Rolling back the outer transaction then puts a back to 1 and unsets b and c: three keys change.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems