← Questions

Versioned key-value vault with consistency check

0
HarveyCoding

A coding interview question asked at Harvey. It tests versioning, key value store and consistency.

Build an in-memory key-value vault whose keys are file-system-like paths (/contracts/acme/nda.pdf) and which keeps every version. Each write creates a new version of the whole vault, any past version can be read and listed, and a consistency check tells whether two versions (or two copies of the vault) hold the same contents and, if not, where they differ.

Part 1: versioned writes and reads

  • put(path: str, value: str) -> int stores a value and returns the new version number. Versions start at 1 and go up by one per write.
  • delete(path: str) -> int removes a path and returns the new version number.
  • get(path: str, version: int | None = None) -> str | None returns the value at that version (default: latest), or None.

Part 2: folders

  • list(folder: str, version: int | None = None) -> list[str] returns the sorted names of the immediate children (files and sub-folders) under folder at that version. Folders exist only while some path below them exists.

Part 3: consistency check

  • diff(v1: int, v2: int) -> dict returns {"added": [...], "removed": [...], "changed": [...]}: the sorted paths present only in v2, only in v1, and in both with different values.
  • fingerprint(version: int) -> str returns a digest of the vault's full contents at that version: equal contents give equal fingerprints, whatever history led to them. Use it to check two copies of the vault quickly before diffing.

Interface

Unless you agree on a different interface with the interviewer, assume the methods above, with version 0 as the empty vault and ValueError for a version that does not exist.

Example

put("/a/x", "1")       ->  1
put("/a/y", "2")       ->  2
put("/a/x", "3")       ->  3
delete("/a/y")         ->  4
get("/a/x", 1)         ->  "1"
get("/a/x")            ->  "3"
get("/a/y")            ->  None
list("/a", 2)          ->  ["x", "y"]
list("/a")             ->  ["x"]
list("/", 4)           ->  ["a"]
diff(1, 4)             ->  {"added": [], "removed": [], "changed": ["/a/x"]}
diff(2, 4)             ->  {"added": [], "removed": ["/a/y"], "changed": ["/a/x"]}
put("/a/x", "1")       ->  5
fingerprint(5) == fingerprint(1)   ->  True     # same contents, different history
fingerprint(5) == fingerprint(2)   ->  False

Constraints

  • Up to 10^5 writes and up to 10^5 paths; reads of old versions should not replay the whole history.
  • Copying the entire vault on every write is too much memory at that size.

Clarifying questions

  • Does a version cover the whole vault, or is it per path?
  • Are reads of old versions frequent, or mostly of the latest?
  • Can a path be both a file and a folder?
  • Must the fingerprint be cheap to update on each write, or may it be computed on demand?

What a strong answer covers

  • Storage that avoids full copies: per-path version history with binary search by version, or a persistent (copy-on-write) tree that shares unchanged subtrees between versions.
  • Listing a folder at a version from that structure, with its cost.
  • A fingerprint that is order-independent: a Merkle hash per folder in the persistent tree (only the changed path's ancestors are rehashed per write), or a sum of per-path hashes.
  • Finding differences top-down by comparing folder hashes and descending only where they differ.
  • Complexity of put, get at an old version, list, diff and fingerprint.

Follow-ups

  • Add snapshot(name) and restore(version).
  • Two replicas applied the same writes but may have diverged. How do you find the differing paths while sending little data between them?
  • Garbage-collect old versions while keeping named snapshots.
  • Make writes safe for several threads.

Where it's asked

What it tests

versioning · key value store · consistency

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding