Implement support for different users sending queries to the system. All users share a common filesystem in the cloud storage, but each user is assigned an individual storage capacity limit.
- add_user(self, user_id: str, capacity: int) -> bool
Should add a new user to 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 | None
Should 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 storage capacity foruser_idif the file is successfully added orNoneotherwise. Note:- All queries calling the
add_fileoperation implemented during Level 1 are run by the user withuser_id = "admin", who has unlimited storage capacity. - Assume that the
copy_fileoperation preserves the ownership of the original file.
- All queries calling the
- update_capacity(self, user_id: str, capacity: int) -> int | None
Should change the maximum storage capacity for the user withuser_id. If the total size of all user's files exceeds the newcapacity, the largest files (sorted lexicographically in case of a tie) should be removed from the storage until the total size of all remaining files will no longer exceed the newcapacity. Returns the number of removed files, orNoneif a user withuser_iddoes not exist.
Examples
The example below shows how these operations should work:
Queries
add_user("user1", 125)- Returns
True; creates user "user1".
- Returns
add_user("user1", 100)- Returns
False; "user1" already exists.
- Returns
add_user("user2", 100)- Returns
True; creates user "user2".
- Returns
add_file_by("user2", "/file.med", 40)- Returns
75.
- Returns
add_file_by("user1", "/dir/file.big", 50)- Returns
45.
- Returns
add_file_by("user1", "/file.med", 30)- Returns
None; file named "/file.med" already exists.
- Returns
copy_file("/file.med", "/dir/another/file.med")- Returns
True; copying preserves ownership.
- Returns
copy_file("/dir/admin/file.med", "/dir/another/another/file.med")- Returns
False; "user1" does not exist.
- Returns
add_file_by("user1", "/dir/file.small", 10)- Returns
5.
- Returns
add_file_by("user1", "/my_folder/file.huge", 100)- Returns
None; "user1" does not have enough capacity.
- Returns
add_file_by("user3", "/my_folder/file.huge", 100)- Returns
None; "user3" doesn't exist.
- Returns
update_capacity("user1", 300)- Returns
0; all files owned by "user1" fit into the new capacity.
- Returns
update_capacity("user2", 50)- Returns
2; the files "/dir/file.big" and "/file.small" should be deleted to fit into the new capacity.
- Returns
update_capacity("user2", 1000)- Returns
None; "user2" does not exist.
- Returns
Level 4
Implement support for file compression.
- compress_file(self, user_id: str, name: str) -> int | None
Should compress the filenameif it belongs touser_id. The compressed file should be replaced with a new file named<name>.COMPRESSED. The size of the newly created file should be equal to half of the original file. The size of all files is guaranteed to be even, so there should be no fractional calculations. It is also guaranteed thatnamefor this operation never points to a compressed file (i.e., it never ends with.COMPRESSED). Compressed files should be owned byuser_id—the owner of the original file. Returns the remaining storage capacity foruser_idif the file was compressed successfully orNoneotherwise. Note:- Because file names can only contain lowercase letters, compressed files cannot be added via
add_file. - It is guaranteed that all
copy_fileoperations will preserve the suffix.COMPRESSED.
- Because file names can only contain lowercase letters, compressed files cannot be added via
- decompress_file(self, user_id: str, name: str) -> int | None
Should revert the compression of the filenameif it belongs touser_id. It is guaranteed thatnamefor this operation always ends with.COMPRESSED. If decompression results in theuser_idexceeding their storage capacity limit or a decompressed version of the file with the given name already exists, the operation fails. Returns the remaining capacity ofuser_idif the file was decompressed successfully orNoneotherwise.
Examples
The example below shows how these operations should work:
Queries
add_user("user1", 1000)- Returns
True.
- Returns
add_user("user2", 5000)- Returns
True.
- Returns
add_file_by("user1", "/dir/file.mp4", 500)- Returns
500.
- Returns
compress_file("user2", "/dir/file.mp4")- Returns
None; user2 does not own the file.
- Returns
compress_file("user1", "/dir/file.mp4")- Returns
750; the file is compressed to 250 bytes.
- Returns
compress_file("user1", "/folder/non_existing_file")- Returns
None; the file does not exist.
- Returns
compress_file("user1", "/dir/file.mp4")- Returns
None; the file is already compressed.
- Returns
get_file_size("/dir/file.mp4.COMPRESSED")- Returns
250.
- Returns
get_file_size("/dir/file.mp4")- Returns
None; the file no longer exists.
- Returns
copy_file("/dir/file.mp4.COMPRESSED", "/file.mp4.COMPRESSED")- Returns
True.
- Returns
add_file_by("user1", "/dir/file.mp4", 500)- Returns
0.
- Returns
decompress_file("user1", "/dir/file.mp4.COMPRESSED")- Returns
None; not enough capacity for decompression.
- Returns
update_capacity("user1", 2000)- Returns
0.
- Returns
decompress_file("user1", "/dir/file.mp4.COMPRESSED")- Returns
750.
- Returns
decompress_file("user2", "/dir/file.mp4.COMPRESSED")- Returns
None; user2 does not own the file.
- Returns
decompress_file("user1", "/dir/file.mp4.COMPRESSED")- Returns
None; the file is already decompressed.
- Returns
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.

