Time Based Key-Value Store
The key idea
set uses a strictly increasing timestamp, that list is already sorted by time, so a get is just a binary search for the largest timestamp that is <= the query time (the floor).Problem
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.
Implement the TimeMap class:
- TimeMap() initializes the object of the data structure.
- set(key, value, timestamp) stores the key with the value at the given time timestamp.
- get(key, timestamp) returns a value such that set was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no values, it returns "".
It is guaranteed that the timestamps passed to set for the same key are strictly increasing, so each key's history is stored in time order.
Constraints
1 <= key.length, value.length <= 100keyandvalueconsist of lowercase English letters and digits1 <= timestamp <= 10^7- All the timestamps
timestampofsetare strictly increasing per key - At most
2 * 10^5calls will be made tosetandget
Examples
Complexity
Time: O(log n) Space: O(n)
See the full solution
- ✓Full worked approach
- ✓Reference code in 5 languages
- ✓Problem-solving tips
- ✓Step-by-step animated visualization
More Design problems
- Binary Search Tree IteratorMEDIUM
- Design Circular QueueMEDIUM
- Design HashMapEASY
- Design HashSetEASY
- Design TwitterMEDIUM
- Detect SquaresMEDIUM
- Encode and Decode StringsMEDIUM
- Implement Queue using StacksEASY
- Implement Stack using QueuesEASY
- Insert Delete GetRandom O(1)MEDIUM
- LFU CacheHARD
- LRU CacheMEDIUM