← Questions
Key-value map with snapshots
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Apple's technical phone screen, last asked Aug 2025. It tests design, versioning and binary-search.
Build a key-value map whose state you can snapshot and read back later. Later writes must never change what an earlier snapshot shows, and snapshots must not copy the whole map.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
set(key: str, value: str) -> None: sets the current value. -
get(key: str) -> str | None: the current value. -
snapshot() -> str: captures the current state and returns an ID. The i-th snapshot returns"snap-i", starting at"snap-0". -
get_at(snapshot_id: str, key: str) -> str | None: the value the key had when that snapshot was taken.Noneif the key wasn't set then or the ID is unknown.
Example
set("color", "red")
set("size", "S")
snapshot() -> "snap-0"
set("color", "blue")
set("color", "green") # only the last write before the next snapshot matters
snapshot() -> "snap-1"
set("shape", "round")
get("color") -> "green"
get_at("snap-0", "color") -> "red"
get_at("snap-1", "color") -> "green"
get_at("snap-1", "shape") -> None # set after snap-1
get_at("snap-9", "color") -> None # unknown snapshot
Constraints
- Up to
10^5operations; any number of snapshots. - Aim for O(log u) per read, where
uis the number of versions of that key.
Clarifying questions
- Is there a delete, and how should a snapshot show a deleted key?
- Are snapshot IDs ordered, and can they be compared?
- Should consecutive snapshots with no writes between them share storage?
- Will reads during writes come from other threads?
What a strong answer covers
- A global epoch that increments on each snapshot; each key keeps a list of
(epoch, value)appended on write, overwriting the last entry when it has the same epoch. -
get_atmaps the ID to its epoch and binary searches the key's list for the last entry at or before it. - Space proportional to the number of writes, not snapshots × keys; why copying the map is O(keys) per snapshot.
- Deletes as tombstone entries.
- Tests: overwrites between snapshots, snapshots with no writes, unknown keys and IDs.
Follow-ups
- Support
delete(key). - Allow dropping old snapshots and reclaiming their versions.
- Make reads safe while another thread writes, and persist the store to disk.
Where it's asked
- Apple · Technical phone screen · Last asked Aug 2025
What it tests
design · versioning · binary-search
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.
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
