Skip to main content

981. Time Based Key-Value Store

medium

Design a key-value store that lets you set values with timestamps and query the most recent value at or before a given time. A great design-leaning warm-up — and a clean fit for binary search.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

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 "".

Constraints

  • 1 <= key.length, value.length <= 100
  • key and value consist of lowercase English letters and digits.
  • 1 <= timestamp <= 10^7
  • All the timestamps timestamp of set are strictly increasing.
  • At most 2 * 10^5 calls will be made to set and get.

Examples

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"

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

Timestamps for set are strictly increasing — so each key's history is naturally sorted by timestamp.

Hint 2

Hash map from key to a list of (timestamp, value) pairs. set is O(1) amortized append.

Hint 3

get needs 'largest timestamp <= queried time'. That's the rightmost-true binary search.

Hint 4

If no entry has timestamp <= query, return empty string.

Solution approach

Reveal approach

Hash map from key to a list of (timestamp, value) pairs. set: append (timestamp, value) — O(1) amortized; timestamps are strictly increasing so the list stays sorted. get: look up the list (return empty if missing). Run rightmost-true binary search on the list for the largest index with timestamps[idx] <= queried time. Standard pattern: lo = 0, hi = len - 1, best = -1. While lo <= hi: mid = lo + (hi - lo) / 2. If timestamps[mid] <= query, best = mid and lo = mid + 1 (try right). Else hi = mid - 1. Return values[best] or empty string if best == -1. set is O(1), get is O(log m) per key where m is the number of values stored for that key.

Complexity

Time
O(1) set, O(log m) get
Space
O(n)

Related patterns

  • binary-search
  • hash-map
  • design

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Google
  • Uber

Practice these live with InterviewChamp.AI

Drill Time Based Key-Value Store and Binary Search problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →