Versioned key-value vault with consistency check
I was asked this
No reports yet. Sign in to say where you were asked it.
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) -> intstores a value and returns the new version number. Versions start at 1 and go up by one per write. -
delete(path: str) -> intremoves a path and returns the new version number. -
get(path: str, version: int | None = None) -> str | Nonereturns the value at that version (default: latest), orNone.
Part 2: folders
-
list(folder: str, version: int | None = None) -> list[str]returns the sorted names of the immediate children (files and sub-folders) underfolderat that version. Folders exist only while some path below them exists.
Part 3: consistency check
-
diff(v1: int, v2: int) -> dictreturns{"added": [...], "removed": [...], "changed": [...]}: the sorted paths present only inv2, only inv1, and in both with different values. -
fingerprint(version: int) -> strreturns 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,getat an old version,list,diffandfingerprint.
Follow-ups
- Add
snapshot(name)andrestore(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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
