A read at time t wants the latest change at or before t
The practice problem starts with a store where set(key, value, time) records a change and get(key, time) returns what the key held at that moment.
Predict
timeout changed to 30 at time 100, 45 at 160 and 60 at 220. What does get(159.5) return?
Here is one key's history, and what five reads return:
timeout's history, in time order
get(99): Noneget(159.5): 30
holds 30from the change at 100
holds 45from the change at 160
holds 60from 220 on
Drawn in order, not to scale. get(99) falls before the change at 100, so it finds nothing. get(159.5) falls before 160, so it still sees the change at 100.
Read
Returns
get(99)
None: nothing at or before 99
get(100)
30: the change at 100 counts
get(159.5)
30: the latest is at 100
get(160)
45
get(10**9)
60
Each read looks back from its own time and takes the first change it meets. A change at the read's own time counts. So get returns the value of the latest change at or before time: that's last_at_or_before, once per key.
Two first ideas don't work:
A dict keyed by (key, time) answers only the exact times that were written. A read at 159.5 finds nothing.
Scanning the key's changes works, and costs O(n). A scan from the newest change is quick for recent times and slow for old ones. On a key with 100,000 changes it took 0.2 µs for a recent time and 1,881.2 µs for an old one. The binary search took 179 to 208 ns either way.