Solution: Time Based Key-Value Store
Explore how to build a time based key-value store that supports storing multiple values for the same key at different timestamps. Learn to implement set and get functions with efficient binary search for retrieval, optimizing time complexity while managing space usage.
Statement
Design a data structure that can store multiple values of the same key at different timestamps and retrieve the key’s value at a certain timestamp.
Implement a struct, TimeStamp, which should provide the following functionalities:
-
NewTimeStamp(): This function initializes the values dictionary and timestamp dictionary.
-
Set Value(key, value, timestamp): This function stores the key and value at any given timestamp.
-
Get Value(key, timestamp): This function returns the value set for this key at the specified timestamp.
Note: When a query requests the value of a key at a timestamp that isn’t recorded, return the value corresponding to the most recent timestamp before the query’s timestamp. If there are no timestamps before the query’s timestamp, return an empty string.
Constraints:
-
key.length,value.length keyandvalueconsist of lowercase English letters and digits.