Versioned Key-Value Store with Commits
Problem statement
Process commands for an in-memory string key-value store.
SET key valuewrites a value.GET keyemits its value orNULL.DELETE keyremoves the key if present.COMMITsaves the current state as the next zero-based version and emits that version id.ROLLBACK versionIdrestores that saved state and emitsOK. 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.