File Storage with User Quotas, Merging, Backups, and Restore
Updated
Problem statement
Build an in-memory file storage system. Every file name is unique across the whole system. You can add and delete files and list the largest files. Each user has a capacity, which limits the space their files can use. Users can be merged. Each user can also back up and restore their own files. The user admin exists from the start and has unlimited capacity. Storage starts empty.
Interface
Process the rows in order and return one string per operation. Each file name is just a string ID. Nothing here uses the real filesystem.
["ADD", name, size]: add a file owned by the admin if the name is not in use. Returntrueif it was added andfalseotherwise.["DELETE", name]: delete the file with that name and free that space for its owner. Return its size in decimal. If no file has that name, return the empty string.["TOP_K", k]: pick up tokfiles across all users, from largest size to smallest. When sizes are equal, put the names in ascending lexicographic order. Write each file asname(size)and join them with,. Return the empty string when there are no files.["ADD_USER", user, capacity]: create a new user with no files and the given total capacity. Returntrue, orfalseif the user already exists.["ADD_FILE_BY", user, name, size]: add a file for an existing user. The name must not be in use. The user's used space after the add must fit within their capacity. Returntrueif the file was added andfalseotherwise. This also works foradmin, whose capacity is unlimited.["MERGE", first, second]: the two users must exist, must be different, and neither may be the admin. Move all files from the second user to the first. Add their total capacities together, then remove the second user. Returntrue. If the users break these rules, returnfalse. The first user's backup stays the same. The second user's backup is thrown away.["BACKUP", user]: save a separate copy of the user's current file names and sizes. It replaces that user's previous backup. Return how many files were saved, or the empty string if the user does not exist. This also works for the admin.["RESTORE", user]: the user must exist. First remove all of the user's current files. Then bring back every file from their latest backup, unless another user now owns that name. Return how many files came back, or the empty string if the user does not exist. With no backup, the user is left with no files. This also works for the admin.
Capacity and snapshot rules
- A user's used space is the total size of their current files. Deleting and restoring change used space. They never change total capacity.
- A backup holds only file names and sizes. Later adds, deletes, merges and restores do not change it. A new backup replaces it, even when the new backup is empty.
- A name skipped because of a conflict stays in the backup. It can be restored later, once the conflict is gone. The user's own current files are removed before conflicts are checked.
- A removed user ID can be created again. It then gets a fresh capacity, no files and no backup. A failed operation leaves the state unchanged.
Function
processFileStorageOperations(operations: String[][]) → String[]Examples
Example 1
operations = [["ADD_USER","alice","10"],["ADD_FILE_BY","alice","a","6"],["ADD_FILE_BY","alice","b","5"],["BACKUP","alice"],["DELETE","a"],["ADD_FILE_BY","alice","b","10"],["RESTORE","alice"],["TOP_K","2"],["ADD_FILE_BY","alice","c","4"],["ADD_FILE_BY","alice","d","1"]]return = ["true","true","false","1","6","true","1","a(6)","true","false"]Alice's file a fits. File b does not fit, so that add fails. The backup stores a of size 6. Then a is deleted, and a bigger file b fits. Restoring removes the current b and brings a back. So a is the only file in the top list. Alice then has exactly 4 bytes available. File c fits, but file d does not.
Example 2
operations = [["ADD_USER","u","10"],["ADD_FILE_BY","u","report","4"],["BACKUP","u"],["DELETE","report"],["ADD","report","9"],["RESTORE","u"],["TOP_K","3"],["BACKUP","admin"],["DELETE","report"],["RESTORE","admin"]]return = ["true","true","1","4","true","0","report(9)","1","9","1"]User u saves report in a backup and then deletes it. The admin then adds a new file named report. When u restores, the administrator owns report, so that saved name is skipped and nothing comes back. Admin files work with backup and restore too. The admin backs up report, deletes it, and restores it.
Example 3
operations = [["ADD_USER","a","10"],["ADD_USER","b","20"],["ADD_FILE_BY","a","a","8"],["BACKUP","a"],["ADD_FILE_BY","b","b","20"],["BACKUP","b"],["MERGE","a","b"],["ADD_FILE_BY","a","c","2"],["RESTORE","a"],["ADD_FILE_BY","a","d","22"],["RESTORE","b"]]return = ["true","true","true","1","true","1","true","true","1","true",""]Users a and b each make a backup. Merging creates a total capacity of 30 and moves file b to user a. File c then fits. Restoring a uses its older backup, so files b and c are removed and only file a of size 8 returns. The total capacity stays 30, so file d fits. User b was removed by the merge, so restoring b returns the empty string.
Constraints
1 <= operations.length <= 1000. Every row is well formed.- Each size is a positive integer at most
10^9. - Each capacity given in the input is a positive integer at most
10^12. 1 <= k <= 1000.- User IDs contain 1 to 30 lowercase ASCII letters or digits.
- File names contain 1 to 50 ASCII letters, digits, slashes, periods, underscores, or hyphens.
- Use signed 64-bit integers for capacities and used space. A merged capacity is at most
10^15.
More Klaviyo interview questions
- Progressive Parcel Tracking OperationsOA · Seen Aug 2026
- Banking Transfers, Account Merges, and Historical BalancesOA · Seen Jul 2025
- In-Memory Database with Permanent and Expiring ValuesOA · Seen Jul 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
- In-Memory File SystemAirbnb · OAAirbnb interview questions
- Power Cell BankOptiver · OAOptiver interview questions
- Four-Level Banking System with Scheduled PaymentsZipRecruiter · OAZipRecruiter interview questions
- Task Management SystemCoinbase · OACoinbase interview questions
- Evolving Merchant Clusters and Persistent PinsStripe · OAStripe interview questions
- Task Management SystemAnthropic · OAAnthropic interview questions
- Versioned Recipe SystemThe Trade Desk · OA
- Recipe Management SystemRamp · OARamp interview questions
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions