Design an in-memory cloud file system
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates understanding of object-oriented design, stateful data structures, and resource management including user quotas, ownership semantics, and file metadata operations.
Constraints
- 1 <= len(operations) <= 10^5
- 0 <= capacity <= 10^9
- 1 <= size <= 10^9
- 0 <= n <= 10^5
- 1 <= len(user_id), len(name) <= 50
- The sum of lengths of all file names appearing in the input is at most 2 * 10^5
- `admin` exists from the start, cannot be re-added, and cannot be removed by `merge_user(x, 'admin')`
- No successful test operation requires returning admin's infinite remaining capacity
Examples
Input: [('add_user', 'alice', 10), ('add_user', 'alice', 5), ('add_file', 'sys.log', 4), ('add_file_by', 'alice', 'a.txt', 3), ('add_file_by', 'alice', 'a.txt', 2), ('add_file_by', 'alice', 'b.txt', 8), ('get_file_size', 'sys.log'), ('get_n_largest', '', 3), ('delete_file', 'a.txt'), ('add_file_by', 'alice', 'b.txt', 8), ('get_n_largest', '', 5)]
Expected Output: [True, False, True, 7, None, None, 4, ['sys.log(4)', 'a.txt(3)'], 3, 2, ['b.txt(8)', 'sys.log(4)']]
Explanation: Covers duplicate users, duplicate file names, insufficient capacity, deleting a file to refund space, and querying the largest files across the whole system with an empty prefix.
Input: [('add_user', 'u1', 5), ('add_user', 'u2', 10), ('add_file_by', 'u1', 'cat', 3), ('add_file_by', 'u2', 'car', 4), ('add_file_by', 'u2', 'cap', 4), ('get_n_largest', 'ca', 5), ('merge_user', 'u1', 'u2'), ('delete_file', 'car'), ('get_n_largest', 'ca', 5), ('merge_user', 'u1', 'u1'), ('merge_user', 'u1', 'u2')]
Expected Output: [True, True, 2, 6, 2, ['cap(4)', 'car(4)', 'cat(3)'], 4, 4, ['cap(4)', 'cat(3)'], None, None]
Explanation: Files with prefix 'ca' are sorted by size descending, then name ascending for ties. After merging, u1 gains u2's remaining capacity and ownership of u2's files.
Hints
- Use multiple hash maps: one for `file -> (size, owner)`, one for `user -> remaining capacity`, and one for `user -> owned file names`.
- For `get_n_largest(prefix, n)`, avoid scanning every file each time by maintaining a mapping from every prefix to the set of file names currently using that prefix.