Problem · Hash Table
Time-Based Key-Value Store
Learn this problemProblem statement
Process timestamped set and get operations on a time-based key-value store.
- A
setstores the supplied value for its key and timestamp and returns"null". - A
getreturns the value for its key with the greatest stored timestamp that is at most the query timestamp. If no such value exists, return the empty string.
Process operations in their array order and return one result per operation.
Function
runTimeMap(operations: String[], keys: String[], values: String[], timestamps: int[]) → String[]Examples
Example 1
operations = ["set","get","get","set","get"]keys = ["foo","foo","foo","foo","foo"]values = ["bar","","","bar2",""]timestamps = [1,1,3,4,4]return = ["null","bar","bar","null","bar2"]Each query returns the latest value for foo whose stored timestamp does not exceed the query timestamp.
Example 2
operations = ["get","set","get"]keys = ["x","x","x"]values = ["","one",""]timestamps = [2,5,4]return = ["","null",""]Neither query has a stored version at or before its timestamp.
Constraints
1 <= operations.length <= 100000, and all four input arrays have the same length.- Each operation is exactly
"set"or"get". 1 <= key.length, value.length <= 100.1 <= timestamp <= 10^9.- For each key, timestamps supplied to
setare strictly increasing.