Define a File Store with User Quotas and Compression
AirbnbSoftware Engineer · Online AssessmentLast updated
Implement an in-memory cloud file store that supports file creation, copying, prefix/suffix search, per-user storage quotas, and atomic file compression/expansion.
Problem Statement
Design an in-memory file storage engine that manages files, user quotas, and file compression. Implement the FileStore class with the following methods:
-
set_user_quota(user_id: str, quota: int) -> boolSets the maximum total storage capacity in bytes thatuser_idcan occupy. If the user already consumes more bytes thanquota, reject the change and returnFalse. Otherwise, apply the new quota and returnTrue. If no quota is set for a user, assume their storage capacity is unlimited. -
add_file(user_id: str, path: str, size: int) -> boolAdds a new file owned byuser_idatpathwithsizebytes. Fails and returnsFalseif a file atpathalready exists, or if adding this file would causeuser_idto exceed their quota. ReturnsTrueon success. -
copy_file(source_path: str, dest_path: str) -> boolCreates a duplicate ofsource_pathatdest_path. The new file is owned by the same user who ownssource_pathand has identical size. Fails and returnsFalseifsource_pathdoes not exist,dest_pathalready exists, or the duplicate causes the owner to exceed their quota. ReturnsTrueon success. -
search_files(prefix: str, suffix: str) -> list[str]Returns a list of all existing file paths that start withprefixand end withsuffix, sorted in ascending lexicographical order. -
compress_file(path: str, factor: float) -> boolResizes the file atpathtofloor(current_size * factor)bytes. The owner's current storage usage must be updated accordingly. Iffactor > 1.0(decompression/expansion) and the increase would cause the owner to exceed their quota, the operation must fail atomically: returnFalseand leave the file size and user quota unchanged. ReturnsFalseif the file does not exist, andTrueon successful update.
Example
fs = FileStore()
assert fs.set_user_quota("u1", 100) == True
assert fs.add_file("u1", "/docs/notes.txt", 60) == True
assert fs.add_file("u1", "/docs/code.py", 50) == False # 60 + 50 > 100
assert fs.copy_file("/docs/notes.txt", "/backup/notes.txt") == False # 60 + 60 > 100
# Compress notes.txt to 50% (30 bytes), making current total usage 30 bytes
assert fs.compress_file("/docs/notes.txt", 0.5) == True
assert fs.copy_file("/docs/notes.txt", "/backup/notes.txt") == True # 30 + 30 <= 100
# Prefix/Suffix search
assert fs.search_files("/", ".txt") == ["/backup/notes.txt", "/docs/notes.txt"]
# Decompression exceeding quota must fail atomically
assert fs.compress_file("/docs/notes.txt", 3.0) == False # (30 * 3) + 30 = 120 > 100
Constraints
1 <= len(user_id), len(path) <= 1001 <= size, quota <= 10^90.0 < factor <= 10.0- Up to
5 * 10^4total operations across all methods.
Hints
Hint 1
Track file metadata (path, owner, size) and user metadata (quota, total_used) using hash maps for O(1) lookups and updates.
Hint 2
For atomic compression, compute the candidate new size and the resulting delta against the owner's quota before mutating any stored values.
Hint 3
For prefix and suffix search, either iterate through all keys if total files are small or maintain a Trie/sorted list if query frequency requires faster sublinear matching.
Follow-up questions
- How would you optimize `search_files` if the file system contained millions of paths and read queries were frequent?
- How would you handle concurrent operations safely without acquiring a coarse global lock across the entire file store?
- If users could belong to shared organization accounts with hierarchical quotas, how would you restructure your quota-tracking model?
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.