Time Based Key-Value Store
Medium Design
TimeMap() keeps values under keys, each with the time it was stored, and can say what a key held at any time. It holds nothing to begin with.
set(key, value, timestamp) stores value under key at the time timestamp. Every call to set has a larger timestamp than every call to set before it, whatever their keys.
get(key, timestamp) returns the value of the latest set of key whose time is no later than timestamp. When there is none, because key was never set or was first set after timestamp, it returns the empty string "".
Example 1
At time 7 the latest set of door is the one at time 5, so red; at 9 it is blue, set at exactly 9. Nothing was set under door by time 4, and nothing ever under lamp, so both answer the empty string.
Example 2
Example 3
Constraints
keyandvalueare 1 to 10 characters, each a lowercase English letter or a digit1 ≤ timestamp ≤ 10⁹- each
sethas a largertimestampthan everysetbefore it - at most
8 × 10⁵calls in all tosetandget
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Design
- LRU CacheAI round · Medium
- Min StackAI round · Medium
- Find Median from Data StreamAI round · Hard
- Serialize and Deserialize Binary TreeHard
- Insert Delete GetRandom O(1)Medium