Time Based Key-Value Store
Design a time-based key-value data structure that can store multiple values for the same key at different timestamps and retrieve the key's value at a specific timestamp.
Implement the TimeMap class:
- TimeMap(): Initializes the data structure.
- void set(String key, String value, int timestamp): Stores the key with the value at the given timestamp.
- String get(String key, int timestamp): Returns the value associated with the largest timestamp_prev such that timestamp_prev <= timestamp. If there are no such values, it returns an empty string "".
(Note: For testing purposes, the input is provided as an array of commands strings and a 2D array of args representing the sequence of method calls.)
Example 1:
Input: commands = ["TimeMap", "set", "get", "get", "set", "get", "get"] args = [[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
Output: [null, null, "bar", "bar", null, "bar2", "bar2"]
Explanation:
set("foo", "bar", 1) stores key "foo" at time 1.
get("foo", 1) returns "bar".
get("foo", 3) returns "bar" (closest past time is 1).
set("foo", "bar2", 4) updates "foo" at time 4.
get("foo", 4) and get("foo", 5) both return "bar2".
Example 2:
Input: commands = ["TimeMap", "set", "get", "get"] args = [[], ["alice", "happy", 10], ["alice", 5], ["alice", 15]]
Output: [null, null, "", "happy"]
Explanation:
get("alice", 5) returns "" because no value was set for "alice" before or at time 5.
Now Your Turn!
Pick the correct output for the given inputInput: commands = ["TimeMap", "set", "set", "get", "get"] args = [[], ["k1", "v1", 10], ["k2", "v2", 20], ["k1", 20], ["k2", 10]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= key.length, value.length <= 100
- key and value consist of lowercase English letters and digits.
- 1 <= timestamp <= 107
- All the timestamps of set are strictly increasing.
- At most 200,000 calls will be made in total to set and get.