Time Based Key-Value Store

Medium Design Binary search

Problem

Design a time-based key-value store. set stores a string value for a key at a timestamp, and get returns the value with the largest timestamp less than or equal to the queried time, or an empty string if there is none. Timestamps on set for the same key are strictly increasing.

Original: LeetCode 981: Time Based Key-Value Store.

Examples

Example 1

Input`TimeMap: set("foo", "bar", 1), get("foo", 1), get("foo", 3), set("foo", "bar2", 4), get("foo", 4), get("foo", 5)`
Output`[null, "bar", "bar", null, "bar2", "bar2"]`

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.

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (1)
  1. LeetCode 981: Time Based Key-Value Storeleetcode.com