In-Memory Database with Permanent and Expiring Values
Updated
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 fromtup to, but not including,t + d, written[t, t + d). It has 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.
More Klaviyo interview questions
- Progressive Parcel Tracking OperationsOA · Seen Aug 2026
- Banking Transfers, Account Merges, and Historical BalancesOA · Seen Jul 2025
- File Storage with User Quotas, Merging, Backups, and RestoreOA · Seen Jun 2025
- Key-Value Locks with Waiting QueuesOA · Seen Sep 2024
- Render Sidebar Labels as HTMLONSITE INTERVIEW · Seen Jul 2025
- Trace Logs at a Two-Second CadenceONSITE INTERVIEW · Seen Jul 2025
- Validate Input Against Length BoundsONSITE INTERVIEW · Seen Jul 2025
- Flatten JSON into Repeated Key-Value PairsONSITE INTERVIEW · Seen Oct 2023
- Conditionally Insert or Update RecordsONSITE INTERVIEW · Seen Jul 2022
Similar questions at other companies
- Split Tender Part 2 - Expiration DatesStripe · OAStripe interview questions
- Organization Reputation After Employee DeparturesHSBC · OAHSBC interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Corporate Card Expense RulesRippling · OA · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Build Sessions from Unordered Events (Part 1)Scale AI · OAScale AI interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
- Process Banking OperationsVercel · OA
- Drop Recent Duplicate MessagesC3.ai · OAC3.ai interview questions