Time Based Key-Value Store
MediumP2Coding
Design a time-versioned key-value store.
Implement the TimeMap class:
TimeMap()creates an empty store.set(key, value, timestamp)storesvalueforkeyattimestamp.get(key, timestamp)returns the value stored forkeywith the largest timestamp that is less than or equal totimestamp. If there is none, return the empty string"".
For each key, set is called with strictly increasing timestamps. get should use binary search.
Example 1
- Input
- ["TimeMap","set","get","get","set","get","get"] [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
- Output
- [null,null,"bar","bar",null,"bar2","bar2"]
- Why
- get(foo, 3) finds the entry at timestamp 1, the latest one not after 3.
Constraints
- 1 <= key.length, value.length <= 100
- 1 <= timestamp <= 10^7
- Timestamps passed to set for one key are strictly increasing
- At most 2 * 10^5 calls in total
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 2 visible tests, Submit all 5. ⌘Enter runs, ⌘⇧Enter submits.