981. Time Based Key-Value Store LeetCode Solution
In this guide, you will get 981. Time Based Key-Value Store LeetCode Solution with the best time and space complexity. The solution to Time Based Key-Value Store problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Time Based Key-Value Store solution in C++
- Time Based Key-Value Store solution in Java
- Time Based Key-Value Store solution in Python
- Additional Resources

Problem Statement of Time Based Key-Value Store
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key’s value at a certain timestamp.
Implement the TimeMap class:
TimeMap() Initializes the object of the data structure.
void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp.
String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no values, it returns "".
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”]
Explanation
TimeMap timeMap = new TimeMap();
timeMap.set(“foo”, “bar”, 1); // store the key “foo” and value “bar” along with timestamp = 1.
timeMap.get(“foo”, 1); // return “bar”
timeMap.get(“foo”, 3); // return “bar”, since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is “bar”.
timeMap.set(“foo”, “bar2”, 4); // store the key “foo” and value “bar2” along with timestamp = 4.
timeMap.get(“foo”, 4); // return “bar2”
timeMap.get(“foo”, 5); // return “bar2”
Constraints:
1 <= key.length, value.length <= 100
key and value consist of lowercase English letters and digits.
1 <= timestamp <= 107
All the timestamps timestamp of set are strictly increasing.
At most 2 * 105 calls will be made to set and get.
Complexity Analysis
- Time Complexity: O(\log n), where n = |\texttt{map[key]}|
- Space Complexity: O(|\texttt{set()}|)
981. Time Based Key-Value Store LeetCode Solution in C++
struct T {
string value;
int timestamp;
};
class TimeMap {
public:
void set(string key, string value, int timestamp) {
map[key].emplace_back(value, timestamp);
}
string get(string key, int timestamp) {
if (!map.contains(key))
return "";
const vector<T>& A = map[key];
int l = 0;
int r = A.size();
while (l < r) {
const int m = (l + r) / 2;
if (A[m].timestamp > timestamp)
r = m;
else
l = m + 1;
}
return l == 0 ? "" : A[l - 1].value;
}
private:
unordered_map<string, vector<T>> map;
};
/* code provided by PROGIEZ */
981. Time Based Key-Value Store LeetCode Solution in Java
class T {
public String value;
public int timestamp;
public T(String value, int timestamp) {
this.value = value;
this.timestamp = timestamp;
}
}
class TimeMap {
public void set(String key, String value, int timestamp) {
map.putIfAbsent(key, new ArrayList<>());
map.get(key).add(new T(value, timestamp));
}
public String get(String key, int timestamp) {
List<T> A = map.get(key);
if (A == null)
return "";
int l = 0;
int r = A.size();
while (l < r) {
final int m = (l + r) / 2;
if (A.get(m).timestamp > timestamp)
r = m;
else
l = m + 1;
}
return l == 0 ? "" : A.get(l - 1).value;
}
private Map<String, List<T>> map = new HashMap<>();
}
// code provided by PROGIEZ
981. Time Based Key-Value Store LeetCode Solution in Python
class TimeMap:
def __init__(self):
self.values = collections.defaultdict(list)
self.timestamps = collections.defaultdict(list)
def set(self, key: str, value: str, timestamp: int) -> None:
self.values[key].append(value)
self.timestamps[key].append(timestamp)
def get(self, key: str, timestamp: int) -> str:
if key not in self.timestamps:
return ''
i = bisect.bisect(self.timestamps[key], timestamp)
return self.values[key][i - 1] if i > 0 else ''
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.