oneby7

Binary Search Searching a sorted array

239. Time Based Key-Value Store

Medium

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) stores value for key at time timestamp. The key's earlier values are kept.
  • get(key, timestamp) returns the value key held at time timestamp. That is the value of the set for key with the largest timestamp that is at most timestamp. If key has no set at or before timestamp, or was never set at all, get returns 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]

Output: ["","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]

Output: ["","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]

Output: ["","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.

ops = ["set", "set", "get", "get", "get"] keys = ["k", "k", "k", "k", "k"] values = ["a", "b"] timestamps = [3, 7, 5, 7, 2]

Constraints

  • 1 ≤ ops.length ≤ 2 × 105
  • Each ops[i] is "set" or "get".
  • keys.length and timestamps.length equal ops.length.
  • values.length equals the number of "set" calls in ops.
  • 1 ≤ keys[i].length, values[i].length ≤ 100
  • Each keys[i] and values[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.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.