Cloud Storage System, Part 1: File Operations
Problem statement
Your task is to implement a simple cloud storage system. All operations that should be supported are listed below.
Solving this task consists of several levels. Subsequent levels are opened when the current level is correctly solved. You always have access to the data for the current and all previous levels.
Requirements
Your task is to implement a simple cloud storage system that maps objects (files) to their metainformation. Specifically, the storage should maintain files and information about them, including each file name and size. This system is in-memory; it does not use the real filesystem.
The levels are cumulative:
- Level 1 supports adding, retrieving, and deleting files.
- Level 2 displays the largest files matching a prefix.
- Level 3 adds capacity-limited users and user merging.
- Level 4 backs up and restores a user's files.
To move to the next level, all tests at the current level must pass.
Note
The queries never call operations that produce a collision between a file name and a directory name.
FastPrep Operation-Sequence Adapter
FastPrep calls the function once with operations. Process the rows from left to right while preserving one shared storage state. Every row starts 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 one element, such as
["10"]. - Encode
Noneas an empty row[]. - Return the formatted list from
GET_N_LARGESTdirectly; an empty list is also[].
Multipart Series
Level 1: File Operations
The cloud storage system should support file manipulation.
add_file(self, name: str, size: int) -> booladds a new filenameto the storage.sizeis the amount of memory required in bytes. The operation fails if a file with the samenamealready exists. ReturnTrueif the file was added successfully orFalseotherwise. The adapter row is["ADD_FILE", name, size]. Files added this way are owned by the unlimitedadminuser.get_file_size(self, name: str) -> int | Nonereturns the size of filenameif it exists, orNoneotherwise. The adapter row is["GET_FILE_SIZE", name].delete_file(self, name: str) -> int | Nonedeletes filename. Return the deleted file size when deletion succeeds, orNoneif the file does not exist. The adapter row is["DELETE_FILE", name].
Function
cloudStorageLevel1(operations: String[][]) → String[][]Examples
Example 1
operations = [["ADD_FILE","/dir1/dir2/file.txt","10"],["ADD_FILE","/dir1/dir2/file.txt","5"],["GET_FILE_SIZE","/dir1/dir2/file.txt"],["DELETE_FILE","/non-existing.file"],["DELETE_FILE","/dir1/dir2/file.txt"],["GET_FILE_SIZE","/not-existing.file"]]return = [["true"],["false"],["10"],[],["10"],[]]The first add succeeds, the duplicate add fails, and the existing file size is 10. Deleting a missing file returns None; deleting the existing file returns 10; the final lookup again returns None.
Constraints
- Every operation row is well formed and uses an operation available at this level.
- Every numeric argument is a base-10 integer string whose value and all arithmetic results fit in a signed 64-bit integer.
- File names, prefixes, and user IDs are non-empty case-sensitive strings.
- The input never creates a collision between a file name and a directory name.
- All users share one global file-name namespace. The
adminuser exists initially and has unlimited capacity. - Process operations in the supplied order. The storage starts empty.