Skip to content
Backend logic · Senior · about 30 minutes

Key-value store with transactions

Implement createStore(), an in-memory key-value store with nested transactions. It returns an object with get(key), set(key, value), delete(key), begin(), commit() and rollback(). get returns the value, or null when the key is missing or deleted. begin starts a transaction inside the current one. rollback undoes every change made since the matching begin. commit ends the innermost transaction and keeps its changes as part of the transaction around it, or in the store when it was the outermost. commit or rollback with no open transaction throws new Error('no transaction'). In the tests an op [at, method, ...args] calls that method at virtual time at (ms); the answer lists what each call returned. Only get returns something, so the answer lists get results and errors.

Start from

function createStore() {
  // Return { get, set, delete, begin, commit, rollback }.
}

Hints and solution

The app has this lab in JavaScript, TypeScript and Python. This page shows the JavaScript code.

Solve it in the app: its tests run without a year.

The hints, the solution and what an interviewer looks for opens with a year in the app. What a year includes