mediumBinary SearchGoogle
Timeline Snapshot Store
A key-value store records writes with timestamps. A read at time t returns the value from the latest write at time ≤ t.
Implement `set(key, value, timestamp)` and `get(key, timestamp)`. Writes arrive in non-decreasing timestamp order. `get` returns the newest value with timestamp ≤ given.
Choose approach
Maphistory[foo]
0→t=1, v=bar
1/16
Narrationstep 1 of 16
set("foo", "bar", 1) → append (1, "bar") to this key's history.
Variables
op=set(foo,bar,1)
size=1
How to think about it
- set: append — amortized O(1).
- get: list is sorted; binary-search for predecessor of the query timestamp.
- Track the best valid candidate as you go right whenever the midpoint is still ≤ target.
- No match → return "".
Edge cases to catch in interview
- Unknown key → "".
- Query before any write for that key → "".
- Multiple keys share the same timestamps — isolated per key.
Examples
Example 1
"bar", "bar", "bar2", "bar2"{"ops":[["set","foo","bar",1],["get","foo",1],["get","foo",3],["set","foo","bar2",4],["get","foo",4],["get","foo",5]]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed