 All Problems
Time Based Key-Value Store
medium
binary search
hash map
design
google
facebook
amazon

Design a time-based key-value data structure that can store multiple values for the same key at different timestamps and retrieve the key's value at a certain timestamp.

Implement TimeMap class:

  • set(key, value, timestamp): Stores the key with value at given timestamp.
  • get(key, timestamp): Returns the value for key at the latest timestamp ≤ given timestamp. If no such value, return "".

Example:

Input:
set love hard 1
get love 1
set love soft 3
get love 3
get love 2
get love 4
Output:
hard
soft
hard
soft

Constraints:

  • 1 ≤ key.length, value.length ≤ 100
  • 1 ≤ timestamp ≤ 10⁷
  • set timestamps are strictly increasing for the same key
  • At most 2 × 10⁵ calls

Input format: One operation per line: "set key value timestamp" or "get key timestamp". Output only get results.

Run to check your code against the sample cases, or submit to run every case