Problem · Hash Table

Transactional In-Memory Redis

Learn this problem
MediumHashiCorp logoHashiCorpFULLTIMEONSITE INTERVIEW

Problem statement

Implement an in-memory key-value store with nested transactions. Process operations in order.

  • ["ADD", key, value] stores or overwrites a string value in the current transaction level.
  • ["DELETE", key] removes the key from the current view, including when the visible value comes from a parent level.
  • ["GET", key] appends the visible value to the result, or NULL when the key is absent.
  • ["BEGIN"] opens a nested transaction.
  • ["COMMIT"] merges the current transaction's changes into its parent and closes it.
  • ["ROLLBACK"] discards the current transaction's changes and closes it.

Only GET produces output. Every COMMIT and ROLLBACK is issued while a transaction is open.

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 inner commit exposes x=3 to its parent transaction. Rolling back that parent restores the base value x=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 deletion masks the base value. The nested add is committed through both open levels and becomes the final base value.

Constraints

  • 1 <= operations.length <= 200000.
  • Every row has exactly the documented arity.
  • Keys and values are non-empty ASCII strings, and values are never NULL.
  • Transaction nesting depth is at most 100000.
  • The total length of all keys and values is at most 2000000.

More HashiCorp problems

drafts saved locally
public String[] runTransactionalRedis(String[][] operations) {
    // Write your code here.
}
operations[["ADD","x","1"],["BEGIN"],["ADD","x","2"],["BEGIN"],["ADD","x","3"],["COMMIT"],["GET","x"],["ROLLBACK"],["GET","x"]]
expected["3", "1"]
checking account