LC 981 ยท Time Based Key-Value Store โ Solution & Explanation | DSA Guide
LeetCode Time Based Key-Value Store โ HashMap + binary search design. Java and Python solutions with visual walkthrough.
LeetCode ยท #981
Time Based Key-Value Store Solution
Design a data structure that stores key-value pairs with timestamps, and retrieves the value at a given key for the latest timestamp โค a query timestamp.
Implement a TimeMap class with: set(key, value, timestamp) โ stores the key-value pair at the given timestamp, and get(key, timestamp) โ returns the value with the largest timestamp โค the given timestamp, or "" if none exists. All set timestamps are strictly increasing.
โข 1 โค key.length, value.length โค 100โข 1 โค timestamp โค 10โทโข All set timestamps are strictly increasing for each keyโข At most 2 ร 10โต calls to set and get
02
Section Two ยท Approach 1
Brute Force โ Linear Scan O(n)
For each get, scan all entries for the key from newest to oldest and return the first with timestamp โค query. O(n) per get where n is the number of entries for that key.
Problem: Linear scan per get with up to 2ร10โต calls. Since timestamps are inserted in increasing order, the list is already sorted โ binary search gives O(log n) per get.
03
Section Three ยท Approach 2
HashMap + Binary Search โ O(log n) per get
Use a HashMap<String, List<(timestamp, value)>>. Since timestamps for each key are strictly increasing, the list is sorted. For get, binary search for the rightmost timestamp โค query (upper bound - 1).
๐ก Mental model: Think of a filing cabinet. Each key has its own folder with timestamped pages in chronological order. When someone asks "what was the value at time T?", you flip to the right page using binary search โ find the last page with a timestamp not exceeding T.
Algorithm
set(key, value, ts): Append (ts, value) to the key's list.
get(key, ts): Binary search the key's list for the rightmost entry where entry.ts โค ts.
If found โ return the value. If not (all timestamps > query) โ return "".
๐ฏ Why rightmost (upper bound)?:
We want the latest timestamp that doesn't exceed the query.
This is a "rightmost โค target" search โ equivalent to upper_bound(ts) - 1 in C++ or bisect_right(ts) - 1 in Python.
Total space: O(S) where S = total number of set calls. Each call stores one (timestamp, value) pair.
07
Section Seven ยท Edge Cases
Edge Cases & Pitfalls
Case
Scenario
Why It Matters
Key not found
get("unknown", 1)
Key doesn't exist in map โ return "".
Timestamp too early
set at ts=5, get at ts=3
No entry โค 3 โ return "".
Exact match
set at ts=5, get at ts=5
Binary search finds exact timestamp โ return its value.
Many entries
10โต sets for one key
Binary search handles large lists in ~17 comparisons.
โ Common Mistake: Using bisect_left instead of bisect_right (or equivalent). We want the rightmost entry โค timestamp. bisect_right finds the first index after all entries โค timestamp โ subtract 1 to get the answer. bisect_left would miss the exact-match case when there are additional concerns.