Python coding

Key-value store with history

Write a class HistoryMap that keeps every version of each key.

  • set(key, value, time) stores value as the version of key at time.
  • get(key, time) returns the value of the newest version of key stored at a time <= time, or None if key had no version yet at that time.

The times given to set are positive integers that strictly increase from one call to the next; a get may ask about any time >= 0. Keys and values are strings; a value may be empty. set must take O(1) amortized time and get O(log v), where v is the number of versions of that key.

The tests call the provided run_history(ops): it applies each operation in order and returns the list of results (None for set, shown as null).

Besides the tests, one large input (10,000 versions, 10,000 lookups) with a ~500 ms limit (scaled to your device's speed): brute force won't pass.

  • run_history([["set","color","red",1],["get","color",1],["get","color",3],["set","color","blue",4],["get","color",4],["get","color",5],["get","color",3]]) → [null,"red","red",null,"blue","blue","red"]
  • run_history([["set","v","1.0",5],["set","v","1.1",8],["set","v","2.0",20],["get","v",7],["get","v",8],["get","v",19],["get","v",100]]) → [null,null,null,"1.0","1.1","1.1","2.0"]
Ctrl/⌘ + Enter runs · Tab indents
Hint 1

Sets arrive in time order, so appending each version to its key's list keeps that list sorted by time, for free.

Hint 2

A get wants the last version with time <= time: the one just before the first version with time > time. That is an upper bound (bisect_right).

Hint 3

Keep times and values in two parallel lists so the search compares plain integers. And test an empty value: "" is a real version, not a miss.

Solution and why it works
pythonO(1) amortized per set, O(log v) per get · O(total versions)
# --- your code ---
from bisect import bisect_right
from collections import defaultdict


class HistoryMap:
    def __init__(self):
        self.times = defaultdict(list)       # key -> times of its versions, increasing
        self.values = defaultdict(list)      # key -> value of each version

    def set(self, key: str, value: str, time: int) -> None:
        self.times[key].append(time)         # times only grow, so each list stays sorted
        self.values[key].append(value)

    def get(self, key: str, time: int) -> str | None:
        i = bisect_right(self.times.get(key, []), time)   # versions [0, i) are not newer
        return self.values[key][i - 1] if i else None


# --- provided: do not edit ---
def run_history(ops: list[list]) -> list:
    store = HistoryMap()
    return [getattr(store, name)(*args) for name, *args in ops]

Times only grow, so each key's versions are appended already sorted and set stays O(1). get(key, t) wants the last version not after t: bisect_right on that key's times finds the first version after t in O(log v), and the answer is the one just before it. The trap is scanning a key's history on every lookup, O(v) per get. Also decide a miss by the index, not by the value's truthiness: an empty string is a stored value.

Related lab: Loops in motion