Skip to main content

Time Based Key-Value Store

MediumPremium

Design a key-value store that remembers every value a key has had over time. Implement the TimeMap class:

  • TimeMap() creates an empty store.
  • set(key, value, timestamp) records that key had value at time timestamp.
  • get(key, timestamp) returns the value key had at time timestamp: the value from the latest set whose timestamp is at most timestamp. If there's no such set, return an empty string.

Timestamps passed to set are strictly increasing across all calls. The tests run a sequence of operations on one store and check the value returned by every get, in order. Timestamps appear as strings in the test input and are converted to integers before each call.

Example 1

Input: ["TimeMap", "set", "get", "get", "set", "get", "get"] [[], ["color", "red", "1"], ["color", "1"], ["color", "3"], ["color", "blue", "4"], ["color", "4"], ["color", "5"]] Output: ["red", "red", "blue", "blue"]

At time 3, the latest value set for color is still "red". From time 4 on, it's "blue".

Example 2

Input: ["TimeMap", "get", "set", "get", "get"] [[], ["a", "1"], ["a", "x", "5"], ["a", "4"], ["b", "5"]] Output: ["", "", ""]

Nothing was set for a at or before time 1 or time 4, and b was never set.

Constraints

  • 1 <= key.length, value.length <= 100
  • key and value contain lowercase English letters and digits.
  • 1 <= timestamp <= 10^7
  • Timestamps passed to set are strictly increasing.
  • At most 2 * 10^5 calls are made to set and get.