File Storage with User Quotas, Merging, Backups, and Restore
Problem statement
Implement an in-memory file storage system with globally unique file names. Support adding and deleting files, selecting the largest files, capacity-limited users, merging users, and per-user backup and restore. The user admin exists initially and has unlimited capacity. Storage starts empty.
Practice interface
The concrete formats and result rules below complete the reported storage task. Process the rows in order and return one string per operation. File names are opaque strings; this exercise does not use the real filesystem.
["ADD", name, size]: add an admin-owned file if the name is unused. Returntrueon success andfalseotherwise.["DELETE", name]: delete an existing file, free its owner's used capacity, and return its size in decimal. Return the empty string if the name is absent.["TOP_K", k]: select up tokfiles across all users, ordered by size descending and name lexicographically ascending. Format each entry asname(size), joined by,. 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 if the name is unused and the resulting used space fits the user's capacity. Returntrueon success andfalseotherwise. This operation also acceptsadmin, whose capacity is unlimited.["MERGE", first, second]: for distinct existing non-admin users, move all files from the second user to the first, add their total capacities, then remove the second user. Returntrue; otherwise returnfalse. The surviving user's backup stays unchanged, and the removed user's backup is discarded.["BACKUP", user]: replace that user's previous backup with an independent snapshot of its current file names and sizes. Return the number saved, or the empty string for a missing user. Admin supports this operation.["RESTORE", user]: for an existing user, first remove all of its current files, then restore every file from its latest backup whose name is not occupied by another user. Return the number restored, or the empty string for a missing user. Without a backup, the user's files remain empty. Admin supports this operation.
Capacity and snapshot rules
- Used space equals the sum of the sizes of the user's current files. Deletion and restoration update used space; they do not change total capacity.
- A backup contains file names and sizes only. It is not changed by later adds, deletes, merges, or restores. A new backup replaces it, including an empty backup.
- A skipped conflicting name remains in the backup and may be restored later after the conflict disappears. A user's own current names are removed before conflict checks.
- A removed user ID may be created again with a fresh capacity and no files or backup. Failed operations leave 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"]The backup stores a of size 6. Restoring removes the current b and brings a back. Alice then has exactly 4 bytes available.
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"]The administrator owns report when u restores, so that saved name is skipped. Admin files participate in backup and restore too.
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",""]Merging creates a total capacity of 30 and transfers b to a. Restoring a keeps its older backup, so only a of size 8 returns; total capacity stays 30. The removed user b cannot restore.
Constraints
1 <= operations.length <= 1000. All rows are well formed.- Sizes are positive integers at most
10^9; supplied capacities are positive integers at most10^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. Merged capacities are at most
10^15.