Binary Search Searching a sorted array
239. Time Based Key-Value Store
Design a key-value store that keeps every value a key has held, each with the time it was stored, and can say
what a key held at any time. Write a class TimeMap with a constructor and two methods:
TimeMap()creates an empty store.set(key, value, timestamp)storesvalueforkeyat timetimestamp. The key's earlier values are kept.get(key, timestamp)returns the valuekeyheld at timetimestamp. That is the value of thesetforkeywith the largest timestamp that is at mosttimestamp. Ifkeyhas nosetat or beforetimestamp, or was never set at all,getreturns the empty string"".
The timestamps of the set calls strictly increase from one call to the next, across all keys. A get can
ask about any time, earlier or later than the calls before it.
Your class is tested by replaying a list of calls on a new TimeMap. ops[i] names the i-th call, "set"
or "get", and keys[i] and timestamps[i] are its key and its timestamp. The sets take their values from
values in order, so the first set stores values[0], the second stores values[1], and so on. The answer
is the list of every value that get returns, in the order of the calls.
In C# and Go the methods are Set and Get. Go creates the store with Constructor(), and Rust with
TimeMap::new(). The names in ops are the same in every language.
Example 1
Input: ops = ["set",
"set",
"set",
"get",
"get",
"get",
"get",
"get",
"get"]
keys = ["door",
"door",
"light",
"door",
"door",
"door",
"door",
"light",
"door"]
values = ["open", "shut", "on"]
timestamps = [4, 9, 12, 3, 4, 8, 9, 20, 100]
["","open","open","shut","on","shut"]Explanation: door holds open from time 4 and shut from time 9. At time 3 it holds nothing yet, so the answer is
"". Time 8 falls between the two sets and gets the earlier value, open. Times 4 and 9 match a set
exactly, and time 100 gets the latest value.
Example 2
Input: ops = ["set",
"get",
"get",
"set",
"get",
"get"]
keys = ["a", "b", "a", "a", "a", "a"]
values = ["x", "y"]
timestamps = [1, 1, 1000000000, 1000000000, 1000000000, 999999999]
["","x","y","x"]Explanation: b was never set, so its answer is "". The first get("a", 1000000000) runs before y is stored, so
the latest value at that point is x. After the second set, the same call returns y.
Example 3
Input: ops = ["set",
"set",
"get",
"set",
"get",
"get",
"get"]
keys = ["cat",
"dog",
"dog",
"cat",
"cat",
"dog",
"cat"]
values = ["tom", "tom", "kit"]
timestamps = [5, 6, 5, 10, 7, 50, 10]
["","tom","tom","kit"]Explanation: dog is first set at time 6, so at time 5 it holds nothing, even though cat was set at time 5. Two keys
can hold the same value.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ ops.length ≤ 2 × 105- Each
ops[i]is"set"or"get". keys.lengthandtimestamps.lengthequalops.length.values.lengthequals the number of"set"calls inops.1 ≤ keys[i].length, values[i].length ≤ 100- Each
keys[i]andvalues[i]is made of lowercase English letters. 1 ≤ timestamps[i] ≤ 109- The timestamps of the
"set"calls strictly increase.
Clarifying questions
What you would ask an interviewer before writing any code.