FastPrepKey-Value Store with Nested Transactions

Key-Value Store with Nested Transactions

Rippling logoRippling● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Implement an in-memory key-value store that processes a sequence of operations.

Each operation is a string array in one of these forms:

  • ["SET", key, value]: assign value to key in the current transaction, or in the base store when no transaction is active.
  • ["GET", key]: read the value visible in the current transaction context. Append that value to the result, or append "NULL" when the key does not exist.
  • ["DELETE", key]: make key absent in the current transaction context.
  • ["BEGIN"]: begin a new transaction nested inside the current context.
  • ["COMMIT"]: commit the innermost transaction into its parent context. When it is the outermost transaction, commit into the base store.
  • ["ROLLBACK"]: discard the innermost transaction.

Only GET operations produce output. Return their results in operation order.

A deletion inside a transaction must hide an older value without deleting that older value from the parent context; rolling back must therefore reveal the parent value again.

Function

processOperations(operations: String[][]) → String[]

Examples

Example 1

operations = [["SET","theme","light"],["GET","theme"],["BEGIN"],["SET","theme","dark"],["GET","theme"],["ROLLBACK"],["GET","theme"]]return = ["light","dark","light"]

The transaction temporarily changes theme to "dark". Rolling it back reveals the base value "light".

Example 2

operations = [["SET","a","1"],["BEGIN"],["DELETE","a"],["GET","a"],["BEGIN"],["SET","a","2"],["COMMIT"],["GET","a"],["ROLLBACK"],["GET","a"]]return = ["NULL","2","1"]

The outer transaction hides a. The nested transaction restores it as "2" and commits that change into the outer transaction. Rolling back the outer transaction restores the base value "1".

Example 3

operations = [["BEGIN"],["SET","x","7"],["BEGIN"],["SET","y","8"],["COMMIT"],["COMMIT"],["GET","x"],["GET","y"],["GET","z"]]return = ["7","8","NULL"]

The inner commit merges y into its parent transaction. The outer commit then persists both keys to the base store.

Constraints

  • 1 <= operations.length <= 10^5.
  • Keys and values are non-empty strings containing at most 50 printable ASCII characters.
  • Every operation has one of the documented forms.
  • Every COMMIT and ROLLBACK has an active transaction.
  • The transaction nesting depth is at most 10^5.

More Rippling problems

See Rippling hiring insights
public String[] processOperations(String[][] operations) {
    // Write your code here.
}
operations[["SET","theme","light"],["GET","theme"],["BEGIN"],["SET","theme","dark"],["GET","theme"],["ROLLBACK"],["GET","theme"]]
expected["light", "dark", "light"]
Checking account…