Skip to content
Decoder

Time Based Key-Value Store

Medium

Problem

Design a class TimeMap that stores several values for the same key at different times, and can look up a key as of a given moment.

- set(key, value, timestamp) stores value for key at time timestamp. - get(key, timestamp) returns the value stored for key with the largest timestamp that is `<=` `timestamp`. If there is none, return "".

Example 1

tm = TimeMap()

Example 2

tm.set("foo", "bar", 1)

Example 3

Input
tm.get("foo", 1)
Output
"bar"

Example 4

Input
tm.get("foo", 3)
Output
"bar"
Why
latest value at or before 3

Example 5

tm.set("foo", "bar2", 4)

Example 6

Input
tm.get("foo", 4)
Output
"bar2"

Example 7

Input
tm.get("foo", 5)
Output
"bar2"

Example 8

Input
tm.get("foo", 0)
Output
""
Why
nothing stored that early

Constraints

  • 1 <= key.length, value.length <= 100
  • 1 <= timestamp <= 10^7
  • The timestamps passed to set are strictly increasing.
  • At most 2 * 10^5 calls to set and get in total.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.