Cloud Storage System
Problem statement
Implement an in-memory cloud storage system. The original assessment exposes a stateful class whose features are unlocked cumulatively across four levels. Do not access the real file system.
FastPrep operation-sequence adapter
FastPrep calls cloudStorageSystem(operations) once. Process the rows of operations from left to right while preserving one shared storage state. Each row begins with an uppercase operation name followed by that method's string arguments.
Return one string-array row for every input operation:
- Encode a Boolean as
["true"]or["false"]. - Encode an integer as a one-element row such as
["10"]. - Encode
Noneas an empty row[]. - For
GET_N_LARGEST, return its list of formatted file strings directly; an empty list is also[].
Level 1: file operations
["ADD_FILE", name, size]implementsadd_file(name, size). Add a new file with the given byte size. Returntrue, orfalseif the name already exists. Files added this way belong to the unlimitedadminuser.["GET_FILE_SIZE", name]implementsget_file_size(name). Return the file size, orNoneif the file does not exist.["DELETE_FILE", name]implementsdelete_file(name). Delete the file and return its former size, or returnNoneif it does not exist.
Level 2: largest files
["GET_N_LARGEST", prefix, n]implementsget_n_largest(prefix, n). Select files whose names start withprefix, order them by size descending and then by name lexicographically ascending, and return at mostnentries formatted asname(size). Return every match when fewer thannexist and an empty list when none exist.
Level 3: users and capacity
All users share the same global file-name space.
["ADD_USER", userId, capacity]implementsadd_user(userId, capacity). Create a user with that byte limit. Returnfalsewhen the user already exists andtrueotherwise.["ADD_FILE_BY", userId, name, size]implementsadd_file_by(userId, name, size). Add the file for that user only when the user exists, the name is free, and the user's total owned size would not exceed the capacity. Return the remaining capacity after success, orNoneon failure.["MERGE_USER", userId1, userId2]implementsmerge_user(userId1, userId2). If both users exist and are distinct, transfer every file owned byuserId2touserId1, combine their capacity limits, deleteuserId2, and returnuserId1's remaining capacity. ReturnNonewhen either user is missing or the IDs are equal. Neither merge argument isadmin.
Level 4: backup and restore
["BACKUP_USER", userId]implementsbackup_user(userId). Replace that user's previous backup with an independent snapshot of the names and sizes of all files currently owned by the user. Return the number of backed-up files, orNonewhen the user does not exist.["RESTORE_USER", userId]implementsrestore_user(userId). ReturnNonewhen the user does not exist. Otherwise delete all files currently owned by the user. If a backup exists, restore each backed-up file whose name is not currently occupied by another user; skip occupied names. With no backup, leave the user with no files. Return the number of restored files.
Normal file additions, deletions, and other users' operations never mutate a stored backup. Merging leaves userId1's backup unchanged and deletes userId2's backup. Restoring files does not change the user's capacity limit.
Function
cloudStorageSystem(operations: String[][]) → String[][]Examples
Example 1
operations = [["ADD_FILE","/dir/file1.txt","5"],["ADD_FILE","/dir/file2","20"],["ADD_FILE","/dir/deeper/file3.mov","9"],["GET_N_LARGEST","/dir","2"],["ADD_FILE","/big_file.mp4","20"],["GET_N_LARGEST","/","2"],["DELETE_FILE","/dir/file1.txt"],["GET_FILE_SIZE","/dir/file1.txt"]]return = [["true"],["true"],["true"],["/dir/file2(20)","/dir/deeper/file3.mov(9)"],["true"],["/big_file.mp4(20)","/dir/file2(20)"],["5"],[]]The first three operations add files. The first ranking returns the two largest names under /dir. After adding /big_file.mp4, the two size-20 files tie, so their names decide the order. Deleting /dir/file1.txt returns its former size, and the following lookup returns an empty row for None.
Example 2
operations = [["ADD_USER","user1","100"],["ADD_USER","user2","40"],["ADD_FILE_BY","user1","/a","60"],["ADD_FILE_BY","user2","/b","10"],["ADD_FILE_BY","user2","/c","30"],["MERGE_USER","user1","user2"],["GET_FILE_SIZE","/b"],["ADD_FILE_BY","user2","/d","1"],["ADD_FILE_BY","user1","/d","40"],["MERGE_USER","user1","user1"]]return = [["true"],["true"],["40"],["30"],["0"],["40"],["10"],[],["0"],[]]Before the merge, user1 has 40 bytes remaining and user2 has none. The merge transfers /b and /c, deletes user2, and leaves user1 with 40 bytes. Calls for the deleted user and a self-merge return empty rows.
Example 3
operations = [["ADD_USER","user","100"],["ADD_FILE_BY","user","/file3.mp4","60"],["ADD_FILE_BY","user","/file4.txt","10"],["BACKUP_USER","user"],["DELETE_FILE","/file3.mp4"],["DELETE_FILE","/file4.txt"],["ADD_FILE","/file3.mp4","140"],["ADD_FILE_BY","user","/dir/file5.new","20"],["RESTORE_USER","user"],["GET_FILE_SIZE","/file3.mp4"],["GET_FILE_SIZE","/file4.txt"],["GET_FILE_SIZE","/dir/file5.new"]]return = [["true"],["40"],["30"],["2"],["60"],["10"],["true"],["80"],["1"],["140"],["10"],[]]The backup remembers /file3.mp4 and /file4.txt. Before restore, the admin occupies the former name and the user owns /dir/file5.new. Restore deletes the user's current file, skips the conflicting name, restores /file4.txt, and returns 1.
Constraints
- Every operation row is well-formed and uses one of the operation names described above.
- The integer arguments from the source are supplied as base-10 strings that fit the signed 64-bit runtime representation.
- The input does not create collisions between file and directory names.
- All users share one file-name namespace;
adminexists initially and has unlimited capacity. - Process operations in the supplied order. The system is empty initially except for
admin.