Transactional Key-Value Store
Updated
Problem statement
Build a key-value store. Keys are strings and values are integers. The store can have one open transaction at a time.
You are given the array operations. Run the operations in order. Each one has one of these forms:
["SET", key, value]: storevalueunder the key. With no open transaction, the change is saved right away. Inside a transaction, the change is only a pending change of that transaction.["GET", key]: look for the key in the transaction's pending changes first, then in the saved data. Return the value as a decimal string, or"null"if the key is not found.["BEGIN"]: open a transaction with no pending changes. The input never hasBEGINwhile a transaction is already open.["COMMIT"]: if a transaction is open, save all its pending changes at once, close it, and return"true". Otherwise return"false".["ROLLBACK"]: if a transaction is open, throw away all its pending changes, close it, and return"true". Otherwise return"false".
SET and BEGIN give no output. Return the results of every GET, COMMIT and ROLLBACK, in the order they happen.
Function
processTransactions(operations: String[][]) → String[]Examples
Example 1
operations = [["SET","item","1"],["GET","item"],["BEGIN"],["SET","item","2"],["GET","item"],["GET","missing"],["COMMIT"],["GET","item"]]return = ["1","2","null","true","2"]The first GET sees the saved value 1. Inside the transaction, the pending value 2 hides it, and the missing key returns null. COMMIT returns true and saves 2, so the last GET returns it too.
Example 2
operations = [["SET","balance","10"],["BEGIN"],["SET","balance","-5"],["GET","balance"],["ROLLBACK"],["GET","balance"],["COMMIT"]]return = ["-5","true","10","false"]Inside the transaction, balance is -5, so the first GET returns it. ROLLBACK throws that change away and returns true, so the next GET sees the saved value 10 again. The last COMMIT returns false because no transaction is open.
Example 3
operations = [["BEGIN"],["SET","draft","7"],["GET","draft"],["ROLLBACK"],["GET","draft"],["SET","draft","9"],["GET","draft"]]return = ["7","true","null","9"]The first write happens inside the transaction, so it is only a pending change. ROLLBACK throws it away, so the next GET returns null. The later write happens outside any transaction, so it is saved right away and the last GET sees it.
Constraints
1 <= operations.length <= 200000.- Every operation has exactly the form its command needs.
- Each key has from
1to64characters, and each character is a lowercase English letter or a digit. - Every value is a signed 32-bit integer, written in its standard decimal form.
BEGINnever appears while a transaction is open.
More Grammarly interview questions
Similar questions at other companies
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Hash Map with Separate ChainingApplied Intuition · ONSITE INTERVIEWApplied Intuition interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Design Authentication ManagerOracle · ONSITE INTERVIEWOracle interview questions