Problem · Design
Time-Based Key-Value Database
Learn this problemProblem statement
Process operations on a time-based key-value database.
Each operation is one of:
["SET", key, value, timestamp]: storevalueforkeyat the integertimestamp.["GET", key, timestamp]: return the value stored forkeyat the greatest timestamp less than or equal to the requested timestamp. Return the empty string when no such value exists.
SET timestamps are strictly increasing for each individual key. GET timestamps may arrive in any order. Return one string for every GET operation, in operation order.
Function
runTimeDatabase(operations: String[][]) → String[]Examples
Example 1
operations = [["SET","foo","bar","1"],["GET","foo","1"],["GET","foo","3"],["SET","foo","bar2","4"],["GET","foo","4"],["GET","foo","5"]]return = ["bar","bar","bar2","bar2"]The first two reads use the value stored at timestamp 1. Once bar2 is stored at timestamp 4, reads at 4 and later return it.
Example 2
operations = [["GET","missing","2"],["SET","a","one","3"],["SET","b","two","5"],["GET","a","2"],["GET","a","10"],["GET","b","5"]]return = ["","","one","two"]A missing key and a query before a key's first timestamp both return the empty string. Keys keep independent histories.
Constraints
1 <= operations.length <= 20000- Every operation has a valid name and arity.
- Keys and stored values are non-empty strings.
0 <= timestamp <= 10^9SETtimestamps are strictly increasing for each key.