Extend cloud file system with copy and compression
MetaSoftware Engineer · Online AssessmentLast updated
This question evaluates understanding of data structures and algorithms for stateful resource management, covering bookkeeping of per-user quotas, file metadata operations (copy, compress/decompress), string filtering and sorting, and efficient selection and deletion of largest elements.
In-Memory Cloud File System V2: Copy, Capacity Updates, Compress/Decompress
Design an in-memory file system with per-user quotas and additional operations.
Data model / rules
- Each file has:
name(unique string)size(positive int)owner(user id)- Each user has:
capacityused- A user may only own files such that
used <= capacity.
API
add_user(user_id, capacity) -> bool
- Create a user with
used = 0.
add_file_by(user_id, name, size) -> bool
- Add a new file owned by
user_id. - Fail if user missing, name exists, or would exceed capacity.
add_file(name, size) -> bool
- Add a file owned by a special
adminuser with effectively unlimited capacity (or explicitly create admin with very large capacity).
copy_file(from_name, to_name) -> bool
- Duplicate an existing file to a new name.
- The copy has the same size and owner as the source.
- Fail if
from_namemissing,to_namealready exists, or the owner’s capacity would be exceeded.
get_file_size(name) -> int | null
- Return size or
nullif missing.
get_n_largest(prefix, suffix, n) -> list<string>
- Filter files with
namestarting withprefixand ending withsuffix. - Return up to
nformatted:"name(size)". - Sort by size desc, then name asc.
update_capacity(user_id, new_capacity) -> bool
- Update the user’s capacity.
- If
used > new_capacity, repeatedly delete the user’s largest files (tie-break by name asc/desc—state your tie-break clearly) untilused <= new_capacity. - Return
falseif user missing.
compress_file(name) -> bool
- Convert a file into a compressed version:
- Only if
nameexists and does not already represent a compressed file. - New file name becomes
name + "compress"(or another fixed suffix; be consistent). - New size becomes
floor(old_size / 2). - Ownership stays the same.
- Replace the original (remove old name, add new name).
- Update the owner’s
usedaccordingly.
decompress_file(name) -> bool
- Only valid if
nameends with the compression suffix. - New name removes the suffix; new size becomes
old_size * 2. - Fail if the decompressed name already exists or if the owner would exceed capacity.
- Replace the compressed file with the decompressed one.
Notes
- Aim for correct bookkeeping of
usedand efficient deletion of largest files (e.g., heap + lazy deletion). - Assume up to ~1e5 operations.
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.