Skip to contentKEEP THE CRAFTTHE SANCTUARY

Time-Based Key-Value Store

Store several timestamped values per key and return the latest one at or before a given time.

The problem

Implement the class TimeMap, a key-value store that remembers every value a key has had over time. - TimeMap() creates an empty store. - set(key, value, timestamp) stores value for key at time timestamp. - get(key, timestamp) returns the value that was set for key at the largest stored timestamp that is less than or equal to timestamp. If key has no value at or before that time, or was never set, return "". For every key, the timestamps passed to set only increase. For example, after set("foo", "bar", 1) and set("foo", "bar2", 4), get("foo", 3) returns "bar", get("foo", 4) returns "bar2", and get("foo", 0) returns "". Constraints: keys and values have 1 to 100 lowercase letters or digits, timestamps are from 1 to 10,000,000, and there are at most 200,000 calls in total.

Starter code

class TimeMap:
    def __init__(self):
        pass

Public examples

  1. obj = TimeMap() obj.set("foo", "bar", 1) obj.get("foo", 1) obj.get("foo", 3) obj.set("foo", "bar2", 4) obj.get("foo", 4) obj.get("foo", 5)gives[None, None, "bar", "bar", None, "bar2", "bar2"]
  2. obj = TimeMap() obj.set("a", "x", 1) obj.set("a", "y", 4) obj.get("a", 3)gives[None, None, None, "x"]
  3. obj = TimeMap() obj.set("a", "x", 1) obj.set("b", "y", 2) obj.get("a", 2) obj.get("b", 2)gives[None, None, None, "x", "y"]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.