Time Based Key-Value Store

Difficulty: Medium

Design a key-value store where each value is tagged with a timestamp instead of being overwritten. It supports two operations:

  • set(key, value, timestamp): store value for key at timestamp. Timestamps for the same key always strictly increase across set calls.
  • get(key, timestamp): return the value stored for key at the largest timestamp less than or equal to timestamp. If nothing was set for that key at or before that time, return an empty string.

Examples

Input: set("foo", "bar", 1), then get("foo", 1)
Output: "bar"

"bar" was stored for "foo" at exactly timestamp 1.

Input: get("foo", 3) (right after the set above, with nothing new set in between)
Output: "bar"

Nothing was set between timestamps 1 and 3, so the most recent value at or before timestamp 3 is still "bar".

Input: set("foo", "bar2", 4), then get("foo", 4) and get("foo", 5)
Output: "bar2" for both

Timestamp 4 introduces a newer value, and it remains the answer for any query timestamp of 4 or later, until something even newer is set.

Constraints

  • 1 <= key.length, value.length <= 100

  • key and value consist of lowercase English letters and digits.

  • 1 <= timestamp <= 10^7

  • All the timestamps passed to set for the same key are strictly increasing.

  • At most 2 * 10^5 calls total will be made to set and get.

Approach

The simplest approach stores each key's (timestamp, value) pairs in a list and, on get, scans it for the latest timestamp that doesn't exceed the query. Timestamps per key always increase, so the list is sorted for free.

Because it's sorted, get doesn't need to check every entry. It's asking "what's the rightmost timestamp <= this value?", a binary search for a boundary rather than an exact match.

Narrow a left/right range as usual. When an entry's timestamp is <= the target, save its value as the best answer so far and keep searching right for a better one.

Solutions

Brute Force — Scan Backwards

Keep each key's (timestamp, value) pairs in order. On get, scan backwards from the newest entry until one is at or before the query timestamp.

class TimeMap {
  constructor() {
    this.store = new Map();
  }

  set(key, value, timestamp) {
    if (!this.store.has(key)) this.store.set(key, []);
    this.store.get(key).push([timestamp, value]);
  }

  get(key, timestamp) {
    const entries = this.store.get(key);
    if (!entries) return "";

    for (let i = entries.length - 1; i >= 0; i--) {
      if (entries[i][0] <= timestamp) {
        return entries[i][1];
      }
    }
    return "";
  }
}

Time: set: O(1). get: O(n) in the worst case, where n is the number of values stored for that key. · Space: O(n) total across all set calls.

Optimal — Binary Search

Binary search the key's sorted (timestamp, value) pairs for the rightmost timestamp <= the query, tracking the best match so far.

class TimeMap {
  constructor() {
    this.store = new Map();
  }

  set(key, value, timestamp) {
    if (!this.store.has(key)) this.store.set(key, []);
    this.store.get(key).push([timestamp, value]);
  }

  get(key, timestamp) {
    const entries = this.store.get(key);
    if (!entries) return "";

    let left = 0;
    let right = entries.length - 1;
    let result = "";

    while (left <= right) {
      const mid = Math.floor((left + right) / 2);

      if (entries[mid][0] <= timestamp) {
        result = entries[mid][1];
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }

    return result;
  }
}

Time: set: O(1). get: O(log n), where n is the number of values stored for that key. · Space: O(n) total across all set calls.