Time Based Key-Value Store
Design a time-based key-value store supporting two operations. set(key, value, timestamp) stores the key with the given value at time timestamp. get(key, timestamp) returns the value that was set for key at the largest stored time_prev <= timestamp; if no such value exists, return the empty string "". For each key, set is called with strictly increasing timestamps.
Open official problem prompt ↗Answer 'what was this key's value as of time T?' quickly, given writes that arrive in time order.
Like reading a document's edit history: to see the text as it stood on a certain date, you jump to the latest revision made on or before that date rather than replaying every edit.
- Input
- set("foo","bar",1); get("foo",1); get("foo",3)
- Output
- "bar", "bar"
- Why
- get(foo,1) matches the value stored exactly at time 1; get(foo,3) finds no later entry, so it falls back to the value at time 1, still "bar".
1 <= key.length, value.length <= 100key and value consist of lowercase English letters and digits1 <= timestamp <= 10^7All set timestamps for a given key are strictly increasingAt most 2*10^5 calls total to set and get