Design and implement a time-based key-value data structure that stores multiple values for the same key at different timestamps and retrieves values based on temporal queries. Unlike the LeetCode version, this interview uses real timestamps (Unix epoch seconds as floats or datetime objects) rather than monotonically increasing integers. The main challenge extends beyond basic implementation to production-grade concerns: writing comprehensive tests, mocking time, ensuring timestamp ordering guarantees, and handling concurrent access with appropriate locking strategies.
Implement a TimeMap class that stores key-value pairs with real timestamp values and supports temporal queries.
TimeMap() - Initializes the data structureset(key: str, value: str, timestamp: float) - Stores the key with the given value at the specified timestamptimestamp is a real Unix timestamp (seconds since epoch) as a floatget(key: str, timestamp: float) -> str - Returns the value where set was called previously with timestamp_prev <= timestamptimestamp_prev"" if no such value exists`from datetime import datetime import time timemap = TimeMap()
t1 = time.time() # e.g., 1678901234.567 timemap.set("user:1:status", "online", t1) time.sleep(0.1) t2 = time.time() $L41 $L43 timemap.get($L44, t2) == $L45 t3 = time.time() timemap.$L47($L48, $L49, t3) $L4b timemap.get($L4c, t2 + $L4d) == $L4e $L50 timemap.get($L51, t3) == $L52 $L54 timemap.get($L55, t1 - $L56) == $L57 $L59 timemap.get($L5a, t3) == $L5b