Transactional In-Memory Redis
Updated
Problem statement
Build an in-memory key-value store that supports nested transactions. Go through operations in order:
["ADD", key, value]: setkeyto the stringvaluein the current transaction. An old value is overwritten.["DELETE", key]: removekeyfrom what the current transaction sees, even when its value was set in an outer transaction.["GET", key]: add the valuekeyhas right now to the result, orNULLif it has none.["BEGIN"]: open a new transaction inside the current one.["COMMIT"]: keep the current transaction's changes, hand them to the transaction around it, and close it.["ROLLBACK"]: undo the current transaction's changes and close it.
Changes made while no transaction is open go straight into the store. Only GET adds to the result. Return the list of GET results in order. COMMIT and ROLLBACK only appear while a transaction is open.
For example, after ADD x 1, BEGIN, ADD x 2, ROLLBACK, a GET x returns 1.
Function
runTransactionalRedis(operations: String[][]) → String[]Examples
Example 1
operations = [["ADD","x","1"],["BEGIN"],["ADD","x","2"],["BEGIN"],["ADD","x","3"],["COMMIT"],["GET","x"],["ROLLBACK"],["GET","x"]]return = ["3","1"]The base store has x = 1. The first transaction sets x = 2, and the inner one sets x = 3. The inner COMMIT hands x = 3 to the first transaction, so GET x returns 3. Then ROLLBACK undoes the first transaction, including the x = 3 it received, so the next GET x returns 1. The result is ["3", "1"].
Example 2
operations = [["ADD","a","base"],["BEGIN"],["DELETE","a"],["GET","a"],["BEGIN"],["ADD","a","inner"],["GET","a"],["COMMIT"],["COMMIT"],["GET","a"]]return = ["NULL","inner","inner"]The base store has a = "base". Inside the first transaction, DELETE a hides it, so GET a returns NULL. The inner transaction sets a = "inner", and GET a returns "inner". Both COMMITs pass a = "inner" all the way out to the base store, so the last GET a returns "inner".
Constraints
1 ≤ operations.length ≤ 200000- Each row has exactly the parts shown above for its operation.
- Keys and values are non-empty ASCII strings. No value is the string
NULL. - At most
100000transactions are open at the same time. - All keys and values together have at most
2000000characters.
More HashiCorp interview questions
Similar questions at other companies
- Key-Value Store with Nested TransactionsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Layer History with Batch Undo and RedoFigma · OA · PHONE SCREENFigma interview questions
- Nested Recipe Cart TransactionsDecagon · PHONE SCREENDecagon interview questions
- 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