37 · Design Data Structures

#981 Time Based Key-Value Store

Mediumsolve on LeetCode ↗

frequently reported atNetflixAppleUberAmazonMicrosoftGoogle

the problem

In plain words

Build TimeMap, a key-value store that remembers history. set(key, value, timestamp) records that key holds value from that moment on. get(key, timestamp) returns what key held at that time: the value from the latest set call whose timestamp is at most the one asked for. If key has no such version, return the empty string. The timestamps passed to set strictly increase.

in set("foo", "bar", 1), get("foo", 1), get("foo", 3), set("foo", "bar2", 4), get("foo", 4), get("foo", 5)

out null, "bar", "bar", null, "bar2", "bar2"

At time 3 the latest version at or before 3 is the one from time 1.

in set("price", "100", 5), get("price", 4), get("stock", 9)

out null, "", ""

Nothing was set for price before time 5, and stock was never set at all.

  • 1 ≤ key.length, value.length ≤ 100 (lowercase letters and digits)
  • 1 ≤ timestamp ≤ 10⁷
  • The timestamps of set calls strictly increase
  • At most 2·10⁵ calls in total

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #35
      Search Insert Position
      Easyin Binary Search
    2. #729
      My Calendar I
      Mediumin BST
    3. #362
      Design Hit Counter
      Mediumin QueuesLeetCode Premium