Cloud Storage System
Read-only note (07-03-2026): This is not a regular single-function coding problem.
The original task is about building a stateful cloud storage system across multiple levels, and it is usually tested with unit tests.
FastPrep currently needs a function signature, example test cases, and runnable code to create a problem page, so those parts were added only as placeholders to fit the page format.
Please ignore the placeholder function, example test cases, editorial solution, sample input/output, and scaffold code. This page is mainly for understanding the original prompt, not for submitting a normal solution.
Thank you for your understanding!
Instructions
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 solved. You always have access to the data for the current and all previous levels.
You are not required to provide the most efficient implementation. Any code that passes the unit tests is sufficient.
You can execute a single test case by running the following command in the terminal: bash run_single_test.sh "<test_case_name>".
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, such as name and size. Note that this system should be in-memory: you do not need to work with the real filesystem.
Plan your design according to the level specifications below:
- Level 1: The cloud storage system should support adding a new file and retrieving and deleting files.
- Level 2: The cloud storage system should support displaying the largest files.
- Level 3: The cloud storage system should support adding users with limited capacities and merging two users.
- Level 4: The cloud storage system should support backing up and restoring a user's files.
To move to the next level, you need to pass all the tests at this level when submitting the solution.
Note: It is guaranteed that the given queries will never call operations that result in collisions between file and directory names.
Level 1
The cloud storage system should support file manipulation.
add_file(self, name: str, size: int) -> boolshould add a new filenameto the storage.sizeis the amount of memory required in bytes. The current operation fails if a file with the samenamealready exists. ReturnsTrueif the file was added successfully orFalseotherwise.get_file_size(self, name: str) -> int | Noneshould return the size of the filenameif it exists, orNoneotherwise.delete_file(self, name: str) -> int | Noneshould delete the filename. Returns the deleted file size if the deletion was successful orNoneif the file does not exist.
The example below shows how these operations should work:
| Queries | Explanations |
|---|---|
add_file("/dir1/dir2/file.txt", 10) | returns True; adds file "/dir1/dir2/file.txt" of size 10 |
add_file("/dir1/dir2/file.txt", 5) | returns False; the file "/dir1/dir2/file.txt" already exists |
get_file_size("/dir1/dir2/file.txt") | returns 10 |
delete_file("/non-existing.file") | returns None; the file "/non-existing.file" does not exist |
delete_file("/dir1/dir2/file.txt") | returns 10 |
get_file_size("/not-existing.file") | returns None; the file "/not-existing.file" does not exist |
Level 2
Implement an operation for retrieving some statistics about files with a specific prefix.
get_n_largest(self, prefix: str, n: int) -> list[str]should return the list of strings representing the names of the topnlargest files with names starting withprefixin the following format:["<name_1>(<size_1>)", ..., "<name_n>(<size_n>)"]. Returned files should be sorted by size in descending order. In case of a tie, they should be sorted in lexicographical order of the names. If there are no such files, return an empty list. If the number of such files is less thann, all of them should be returned in the specified format.
The example below shows how these operations should work:
| Queries | Explanations |
|---|---|
add_file("/dir/file1.txt", 5) | returns True |
add_file("/dir/file2", 20) | returns True |
add_file("/dir/deeper/file3.mov", 9) | returns True |
get_n_largest("/dir", 2) | returns ["/dir/file2(20)", "/dir/deeper/file3.mov(9)"] |
get_n_largest("/dir/file", 3) | returns ["/dir/file2(20)", "/dir/file1.txt(5)"] |
get_file_size("/another_dir/file.txt") | returns None; there are no files with this name |
add_file("/big_file.mp4", 20) | returns True |
get_n_largest("/", 2) | returns ["/big_file.mp4(20)", "/dir/file2(20)"] |
Level 3
Implement support for queries from different users. All users share a common filesystem in the cloud storage system, but each user is assigned a storage capacity limit.
add_user(self, user_id: str, capacity: int) -> boolshould add a new user in the system, withcapacityas their storage limit in bytes. The total size of all files owned byuser_idcannot exceedcapacity. The operation fails if a user withuser_idalready exists. ReturnsTrueif a user withuser_idis successfully created, orFalseotherwise.add_file_by(self, user_id: str, name: str, size: int) -> int | Noneshould behave in the same way as theadd_filefrom Level 1, but the added file should be owned by the user withuser_id. A new file cannot be added to the storage if doing so will exceed the user'scapacitylimit. Returns the remaining capacity of the user if the file is added successfully, orNoneotherwise.
Note that all queries calling the add_file operation implemented during Level 1 are run by the user with user_id = "admin", who has unlimited storage capacity.
merge_user(self, user_id_1: str, user_id_2: str) -> int | Noneshould merge the account ofuser_id_2withuser_id_1. Ownership of all ofuser_id_2's files is transferred touser_id_1, and any remaining storage capacity is also added touser_id_1's limit.user_id_2is deleted if the merge is successful. Returns the remaining capacity ofuser_id_1after merging, orNoneif one of the users does not exist oruser_id_1is equal touser_id_2. It is guaranteed that neitheruser_id_1noruser_id_2equals"admin".
Level 4
Implement support to allow users to back up their files.
backup_user(self, user_id: str) -> int | Noneshould back up the current state of all files owned byuser_id, i.e., file names and sizes. The backup is stored on a separate storage system and is not affected by any new file manipulation queries. Overwrites any backups for the same user if previous backups exist. Returns the number of backed-up files, orNoneifuser_iddoes not exist.restore_user(self, user_id: str) -> int | Noneshould restore the state ofuser_id's files to the latest backup. If there was no backup, all ofuser_id's files are deleted. If a file cannot be restored because another user added another file with the same name, it is ignored. Returns the number of the files that are successfully restored orNoneifuser_iddoes not exist.
Note that merge_user does not affect user_id_1's backup, and user_id_2 is deleted along with its backup.
Note that the restore_user operation does not affect the user's capacity.
The example below shows how these operations should work:
| Queries | Explanations |
|---|---|
add_user("user", 100) | returns True; creates "user" with 100 bytes capacity limit |
add_file_by("user", "/dir/file1", 50) | returns 50 |
add_file_by("user", "/file2.txt", 30) | returns 20 |
restore_user("user") | returns 0; removes all of "user"'s files |
add_file_by("user", "/file3.mp4", 60) | returns 40 |
add_file_by("user", "/file4.txt", 10) | returns 30 |
backup_user("user") | returns 2; backs up all of "user"'s files |
delete_file("/file3.mp4") | returns 60 |
delete_file("/file4.txt") | returns 10 |
add_file("/file3.mp4", 140) | returns True |
add_file_by("user", "/dir/file5.new", 20) | returns 80 |
restore_user("user") | returns 1; restores "/file4.txt" and deletes "/dir/file5.new" |
placeholder = "read-only" return = "overview-only"
This sample input/output is only a FastPrep formatting placeholder. The original assessment is a stateful multi-method class problem graded by unit tests, so please ignore this runnable example and use the statement above as the source overview.
Read-only upload note: the original screenshots did not provide a single-function input format or numeric constraints for this page format. Any runnable scaffold here is only a placeholder.
public String cloudStorageSystemOverview(String placeholder) {
// This page is read-only. The real task is to implement the CloudStorage class methods described in the statement.
}