Time Based Key-Value Store - Problem
Design a time-based key-value data structure that can store multiple values for the same key at different timestamps and efficiently retrieve values based on time queries.
Your task is to implement the TimeMap class with the following operations:
- TimeMap(): Initialize an empty time-based key-value store
- set(key, value, timestamp): Store the key with the given value at the specified timestamp
- get(key, timestamp): Retrieve the value associated with key at the largest timestamp that is less than or equal to the given timestamp. If no such timestamp exists, return an empty string.
Example scenario:
timeMap.set("stock_price", "100", 1)
timeMap.set("stock_price", "105", 5)
timeMap.set("stock_price", "110", 10)
timeMap.get("stock_price", 3) → "100" (latest value ≤ timestamp 3)
timeMap.get("stock_price", 7) → "105" (latest value ≤ timestamp 7)
timeMap.get("stock_price", 15) → "110" (latest value ≤ timestamp 15)This problem simulates real-world scenarios like stock price tracking, sensor data logging, or database snapshots where you need to query historical data efficiently.
Input & Output
example_1.py — Basic Operations
$
Input:
["TimeMap", "set", "get", "get", "set", "get", "get"]
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
›
Output:
[null, null, "bar", "bar", null, "bar2", "bar2"]
💡 Note:
TimeMap timeMap = new TimeMap(); timeMap.set("foo", "bar", 1); returns "bar" at timestamp 1, returns "bar" at timestamp 3 (latest ≤ 3), timeMap.set("foo", "bar2", 4); returns "bar2" at timestamp 4, returns "bar2" at timestamp 5 (latest ≤ 5)
example_2.py — Multiple Keys
$
Input:
["TimeMap", "set", "set", "get", "get", "get"]
[[], ["love", "high", 10], ["love", "low", 20], ["love", 5], ["love", 10], ["love", 15]]
›
Output:
[null, null, null, "", "high", "high"]
💡 Note:
No value exists before timestamp 10, so get("love", 5) returns empty string. get("love", 10) returns "high" (exact match). get("love", 15) returns "high" (latest value ≤ 15)
example_3.py — Edge Case - Empty Key
$
Input:
["TimeMap", "get", "set", "get"]
[[], ["key2", 1], ["key2", "hello", 1], ["key2", 2]]
›
Output:
[null, "", null, "hello"]
💡 Note:
Querying a non-existent key returns empty string. After setting the value, queries return the appropriate value based on timestamp.
Visualization
Tap to expand
Understanding the Visualization
1
Storage Structure
Each key has an ordered timeline of (timestamp, value) pairs
2
Set Operation
Add new (timestamp, value) pair to the key's timeline
3
Get Operation
Binary search the timeline to find the largest timestamp ≤ query
4
Result
Return the value associated with that timestamp
Key Takeaway
🎯 Key Insight: Since timestamps come in increasing order, we can maintain sorted lists and use binary search for efficient O(log n) retrieval, making this optimal for time-series data queries.
Time & Space Complexity
Time Complexity
O(n)
Each get operation requires scanning through all n timestamp entries for that key
✓ Linear Growth
Space Complexity
O(n)
Storage for all timestamp-value pairs across all keys
⚡ Linearithmic Space
Constraints
- 1 ≤ key.length, value.length ≤ 100
- key and value consist of lowercase English letters and digits
- 1 ≤ timestamp ≤ 107
- All the timestamps in set operations are strictly increasing
- At most 2 × 105 calls will be made to set and get
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code