Problem · Design

Cloud Storage System

MediumAnthropicOA

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) -> bool should add a new file name to the storage. size is the amount of memory required in bytes. The current operation fails if a file with the same name already exists. Returns True if the file was added successfully or False otherwise.
  • get_file_size(self, name: str) -> int | None should return the size of the file name if it exists, or None otherwise.
  • delete_file(self, name: str) -> int | None should delete the file name. Returns the deleted file size if the deletion was successful or None if the file does not exist.

The example below shows how these operations should work:

QueriesExplanations
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 top n largest files with names starting with prefix in 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 than n, all of them should be returned in the specified format.

The example below shows how these operations should work:

QueriesExplanations
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) -> bool should add a new user in the system, with capacity as their storage limit in bytes. The total size of all files owned by user_id cannot exceed capacity. The operation fails if a user with user_id already exists. Returns True if a user with user_id is successfully created, or False otherwise.
  • add_file_by(self, user_id: str, name: str, size: int) -> int | None should behave in the same way as the add_file from Level 1, but the added file should be owned by the user with user_id. A new file cannot be added to the storage if doing so will exceed the user's capacity limit. Returns the remaining capacity of the user if the file is added successfully, or None otherwise.

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 | None should merge the account of user_id_2 with user_id_1. Ownership of all of user_id_2's files is transferred to user_id_1, and any remaining storage capacity is also added to user_id_1's limit. user_id_2 is deleted if the merge is successful. Returns the remaining capacity of user_id_1 after merging, or None if one of the users does not exist or user_id_1 is equal to user_id_2. It is guaranteed that neither user_id_1 nor user_id_2 equals "admin".

Level 4

Implement support to allow users to back up their files.

  • backup_user(self, user_id: str) -> int | None should back up the current state of all files owned by user_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, or None if user_id does not exist.
  • restore_user(self, user_id: str) -> int | None should restore the state of user_id's files to the latest backup. If there was no backup, all of user_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 or None if user_id does 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:

QueriesExplanations
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"
Examples
01 · Example 1
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.

Constraints

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.

More Anthropic problems
drafts saved locally
public String cloudStorageSystemOverview(String placeholder) {
  // This page is read-only. The real task is to implement the CloudStorage class methods described in the statement.
}
placeholder"read-only"
expected"overview-only"
checking account