Design an in-memory cloud file system
MetaSoftware Engineer · Online AssessmentLast updated
This question evaluates understanding of object-oriented design, stateful data structures, and resource management including user quotas, ownership semantics, and file metadata operations.
In-Memory Cloud File System (OOD)
Design an in-memory cloud file system that supports files owned by users with storage quotas.
Data model / rules
- Each file has a unique
name(string) and asize(positive integer). - Each file is owned by exactly one user.
- Each user has a remaining capacity (how many bytes they can still store).
- Special user
"admin"exists and has infinite capacity. - Adding a file consumes the owner’s remaining capacity by
size. - Deleting a file refunds its size back to the owner’s remaining capacity.
API to implement
Implement a class with the following methods (exact names not required, behavior is):
add_user(user_id, capacity) -> bool
- Create a new user with the given remaining capacity.
- Return
falseif the user already exists.
add_file(name, size) -> bool
- Convenience method: add a file owned by
admin. - Return
falseif the file already exists.
add_file_by(user_id, name, size) -> int | null
- Add a file owned by
user_id. - Return the user’s remaining capacity after adding.
- Return
nullif: user_iddoes not existnamealready existssizeexceeds the user’s remaining capacity
get_file_size(name) -> int | null
- Return the file size, or
nullif not found.
delete_file(name) -> int | null
- Delete the file.
- Return the deleted file’s size, or
nullif not found.
get_n_largest(prefix, n) -> list<string>
- Consider only file names starting with
prefix. - Return up to
nfiles formatted as:"<name>(<size>)". - Sort by:
- size descending
- name ascending (lexicographic) for ties
merge_user(user_id_1, user_id_2) -> int | null
- Merge
user_id_2intouser_id_1. - All files owned by
user_id_2become owned byuser_id_1. user_id_1’s remaining capacity increases byuser_id_2’s remaining capacity.- Delete
user_id_2. - Return
user_id_1’s new remaining capacity. - Return
nullif either user doesn’t exist or the IDs are the same.
backup_user(user_id) -> int | null
- Store a snapshot of the current set of files owned by
user_id(name -> size). - Return the number of files included in this backup.
- Return
nullif the user doesn’t exist.
restore_user(user_id) -> int | null
- Restore
user_id’s files from the most recent backup for that user. - First, remove all files currently owned by
user_id. - Then, for each file in the backup, restore it only if its name is not currently used by some other user.
- Return the number of files actually restored.
- If the user exists but has no backup, return
0. - If the user doesn’t exist, return
null.
Notes
- Assume up to ~1e5 operations.
- You may choose any internal data structures, but must satisfy the behavior above.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.