← Questions
Cloud storage with user quotas and backups
0
I was asked this
No reports yet. Sign in to say where you were asked it.
Design an in-memory cloud storage service. The interview has four parts. Each part adds methods, and every earlier method must keep working. Implement a class CloudStorage.
File names are full path strings such as /photos/cat.png, compared exactly. Sizes are positive integers in bytes.
Part 1: files
-
add_file(name: str, size: int) -> bool: add a file. ReturnFalseif a file with that name already exists. -
copy_file(name_from: str, name_to: str) -> bool: copy a file to a new name. Fail ifname_fromdoes not exist orname_toalready exists. -
get_file_size(name: str) -> int: return the file's size, or-1if it does not exist.
Part 2: search
-
find_files(prefix: str, suffix: str) -> list[str]: return up to 10 files whose names start withprefixand end withsuffix, formatted as"name(size)". Order by size descending, then by name ascending.
Part 3: users and quotas
-
add_user(user_id: str, capacity: int) -> bool: create a user with a storage limit. ReturnFalseif the user exists. -
add_file_by(user_id: str, name: str, size: int) -> int | None: add a file owned by the user. Return the user's remaining capacity, orNoneif the user does not exist, the name is taken, or the file would exceed the limit. -
update_capacity(user_id: str, capacity: int) -> int | None: change the limit. If the user is now over it, delete their largest files (ties: smaller name first) until they fit. Return the number of files deleted, orNonefor an unknown user. - Files added with
add_filebelong to a built-inadminuser with no limit. A copy belongs to the source file's owner and counts against that owner's limit; the copy fails if it does not fit.
Part 4: merge, backup, restore
-
merge_user(target_id: str, source_id: str) -> int | None: move all of the source's files and capacity to the target, then delete the source and its backup. Return the target's remaining capacity, orNoneif either user is unknown or they are the same. -
backup_user(user_id: str) -> int | None: save a snapshot of the user's files, replacing any earlier snapshot. Return the number of files saved. -
restore_user(user_id: str) -> int | None: delete the user's current files and re-create the files from the last snapshot (with no snapshot, the user ends up with no files). Skip any file whose name now belongs to another user. Return the number of files restored.
Example
add_file("/docs/a.txt", 100) -> True
copy_file("/docs/a.txt", "/docs/b.txt") -> True
add_file("/img/d.txt", 300) -> True
find_files("/", ".txt") -> ["/img/d.txt(300)", "/docs/a.txt(100)", "/docs/b.txt(100)"]
add_user("u1", 500) -> True
add_file_by("u1", "/u1/x", 200) -> 300
add_file_by("u1", "/u1/y", 250) -> 50
add_file_by("u1", "/u1/z", 60) -> None
update_capacity("u1", 300) -> 1 (deletes /u1/y)
get_file_size("/u1/y") -> -1
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding


