FastPrepVersioned Key-Value Store with Commits

Versioned Key-Value Store with Commits

Otter.ai logoOtter.ai● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Process commands for an in-memory string key-value store.

  • SET key value writes a value.
  • GET key emits its value or NULL.
  • DELETE key removes the key if present.
  • COMMIT saves the current state as the next zero-based version and emits that version id.
  • ROLLBACK versionId restores that saved state and emits OK. Saved versions remain available, and later writes form a new working branch.

Return outputs from GET, COMMIT, and ROLLBACK commands in command order.

Function

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

Examples

Example 1

operations = ["SET a one","GET a","COMMIT","SET a two","GET a","ROLLBACK 0","GET a"]return = ["one","0","two","OK","one"]

Version 0 stores a=one; rolling back restores it.

Example 2

operations = ["GET missing","SET x 1","DELETE x","GET x"]return = ["NULL","NULL"]

Missing and deleted keys both read as NULL.

Example 3

operations = ["SET x 1","COMMIT","SET y 2","COMMIT","ROLLBACK 0","GET y","ROLLBACK 1","GET y"]return = ["0","1","OK","NULL","OK","2"]

Earlier and later committed snapshots remain independently addressable.

Constraints

  • 1 <= operations.length <= 10^4.
  • Keys and values are nonempty strings without spaces.
  • Every ROLLBACK names an existing version.

More Otter.ai problems

See Otter.ai hiring insights
public String[] versionedStore(String[] operations) {
    // Write your solution here.
}
operations["SET a one","GET a","COMMIT","SET a two","GET a","ROLLBACK 0","GET a"]
expected["one", "0", "two", "OK", "one"]
Checking account…