In-Memory Database with Permanent and Expiring Values
Problem statement
Implement an in-memory key-value database with permanent and time-limited writes. Process the given operations in order and return one string result for each operation.
Operations
["SET", time, key, value]: storevalueunderkeywithout an expiration time. Replace any earlier value and deadline. Return"".["SET_TTL", time, key, value, ttl]: storevalueunderkeywith expiration timetime + ttl. Replace any earlier value and deadline. Return"".["GET", time, key]: return the current value, or"null"when the key is absent or expired.["DELETE", time, key]: remove the current value and return"true". Return"false"when the key is absent or expired.
Expiration and ordering
- A time-limited value written at time
twith lifetimedis available during[t, t + d). It is already expired att + d. - A value written by ordinary
SETnever expires. It remains until another write replaces it or a successful deletion removes it. - Expiration is checked before processing an operation at that time. Overwritten and deleted values never reappear.
- Operation times are nondecreasing. Operations with equal times are processed in their input order.
- All times and lifetimes are decimal strings inside the rows. Return the results in the same order as the operations.
Function
processDatabaseOperations(operations: String[][]) → String[]Examples
Example 1
operations = [["SET_TTL","1","session","open","4"],["GET","4","session"],["GET","5","session"],["SET","5","mode","quiet"],["GET","100","mode"]]return = ["","open","null","","quiet"]session is valid from time 1 through times strictly less than 5. It is absent at time 5. The ordinary SET for mode has no expiration.
Example 2
operations = [["SET_TTL","10","item","old","2"],["SET","11","item","new"],["GET","50","item"],["DELETE","50","item"],["GET","50","item"]]return = ["","","new","true","null"]The ordinary SET replaces both the old value and its deadline. The replacement survives time 12. At time 50, the operations run in their listed order.
Constraints
1 <= operations.length <= 100000.- Every row is one of the four valid operation forms above.
- Times are nondecreasing integers in
[0, 10^15]. - Lifetimes are integers in
[1, 10^15]. - Keys and values contain
1to50ASCII letters, digits, or underscores.