Start learning free

LESSON 328 OF 333 · OBJECT-ORIENTED DESIGN - INTERVIEWS

Key-Value Store with Transactions in Swift

Practice O(1)

This is a design round. You write the classes, and the interviewer checks that they behave.

Here you build Store, a store of keys and values. It supports transactions: a group of changes that you can keep or undo together.

Implement the class:

  • Store() starts with no keys and no open transaction.
  • set(key, value) stores value under key. It returns nothing.
  • get(key) returns the value of key, or "NULL" if the key isn't there.
  • remove(key) removes key. It returns true if the key was there.
  • begin() opens a new transaction. It returns nothing.
  • rollback() undoes the innermost transaction and closes it.
  • commit() keeps the changes of the innermost transaction and closes it.
  • depth() returns how many transactions are open.

A call to set adds the key, or replaces its value if the key is already there. A call to remove on a missing key returns false and changes nothing.

A change is a call to set, or a call to remove that returns true.

Transactions can be nested. A call to begin opens a new transaction inside the ones already open. The innermost transaction is the one opened last.

A call to rollback undoes every change made since the innermost transaction began. Every key gets back the value it had then. A key that wasn't there then is removed. Then that transaction closes.

A call to commit closes the innermost transaction and keeps its changes. Only that one transaction closes. The ones around it stay open.

The kept changes now belong to the transaction around it, if there is one. If that transaction is rolled back later, these changes are undone too. With no transaction around it, the changes stay for good.

With no transaction open, rollback and commit return false and change nothing. Otherwise they return true. A change made with no transaction open can never be undone.

The calls run in order on one new Store. A call to set or begin writes nothing. A call to get writes the value or NULL. A call to remove, rollback or commit writes True or False. A call to depth writes the number.

 

Example 1:

Input:
Store()
1. set("color", "red")
2. begin()
3. set("color", "blue")
4. set("color", "green")
5. set("size", "big")
6. get("color")
7. depth()
8. rollback()
9. get("color")
10. get("size")
11. depth()
Output:
green
1
True
red
NULL
0
Explanation:
Call 1 happens before any transaction, so nothing can undo it.
Calls 3 to 5 change color twice and add size.
The rollback in call 8 undoes all three changes.
So color is red again, and size is gone.

Example 2:

Input:
Store()
1. begin()
2. set("fruit", "apple")
3. begin()
4. set("fruit", "pear")
5. commit()
6. depth()
7. get("fruit")
8. rollback()
9. get("fruit")
10. begin()
11. set("fruit", "plum")
12. commit()
13. rollback()
14. get("fruit")
Output:
True
1
pear
True
NULL
True
False
plum
Explanation:
Calls 1 and 3 open two transactions, one inside the other.
The commit in call 5 closes only the inner one. The outer one stays open.
Its change, fruit = pear, now belongs to the outer transaction.
So the rollback in call 8 undoes both pear and apple.
There was no fruit before call 1, so call 9 writes NULL.
Call 12 commits with no transaction around it. Its change stays for good.
Call 13 returns False, because no transaction is open.

Example 3:

Input:
Store()
1. commit()
2. set("pet", "cat")
3. begin()
4. remove("pet")
5. remove("pet")
6. get("pet")
7. set("toy", "ball")
8. remove("toy")
9. rollback()
10. get("pet")
11. get("toy")
12. rollback()
Output:
False
True
False
NULL
True
True
cat
NULL
False
Explanation:
Call 1 returns False, because no transaction is open.
Call 5 returns False, because pet is already gone.
Calls 7 and 8 add toy and then remove it again.
The rollback in call 9 brings pet back and leaves toy out.
Call 12 returns False, because the only transaction is already closed.

 

Constraints:

  • A key has 1 to 10 lowercase English letters.
  • A value has 1 to 10 lowercase English letters.
  • At most 100,000 calls follow the constructor.
  • At most 1,000 transactions are open at once.

Note:

Each call to set, get or remove should run in O(1) time. So should each call to begin and depth. A call to rollback or commit should take O(k) time for the k changes in that transaction. Keep a log per open transaction with the old value of each key it changes. A change made with no transaction open needs no log. Copying the whole store at each begin would take O(n) time for n keys.

STEP 1 OF 26
    Ctrl + Enter to check

    Kv Transactions in Swift

    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free