Katkat

Design an in-memory cloud file system

MetaSoftware Engineer · Online Assessment

Last updated

This question evaluates understanding of object-oriented design, stateful data structures, and resource management including user quotas, ownership semantics, and file metadata operations.

In-Memory Cloud File System (OOD)

Design an in-memory cloud file system that supports files owned by users with storage quotas.

Data model / rules

  • Each file has a unique name (string) and a size (positive integer).
  • Each file is owned by exactly one user.
  • Each user has a remaining capacity (how many bytes they can still store).
  • Special user "admin" exists and has infinite capacity.
  • Adding a file consumes the owner’s remaining capacity by size.
  • Deleting a file refunds its size back to the owner’s remaining capacity.

API to implement

Implement a class with the following methods (exact names not required, behavior is):

  1. add_user(user_id, capacity) -> bool
  • Create a new user with the given remaining capacity.
  • Return false if the user already exists.
  1. add_file(name, size) -> bool
  • Convenience method: add a file owned by admin.
  • Return false if the file already exists.
  1. add_file_by(user_id, name, size) -> int | null
  • Add a file owned by user_id.
  • Return the user’s remaining capacity after adding.
  • Return null if:
  • user_id does not exist
  • name already exists
  • size exceeds the user’s remaining capacity
  1. get_file_size(name) -> int | null
  • Return the file size, or null if not found.
  1. delete_file(name) -> int | null
  • Delete the file.
  • Return the deleted file’s size, or null if not found.
  1. get_n_largest(prefix, n) -> list<string>
  • Consider only file names starting with prefix.
  • Return up to n files formatted as: "<name>(<size>)".
  • Sort by:
  1. size descending
  2. name ascending (lexicographic) for ties
  1. merge_user(user_id_1, user_id_2) -> int | null
  • Merge user_id_2 into user_id_1.
  • All files owned by user_id_2 become owned by user_id_1.
  • user_id_1’s remaining capacity increases by user_id_2’s remaining capacity.
  • Delete user_id_2.
  • Return user_id_1’s new remaining capacity.
  • Return null if either user doesn’t exist or the IDs are the same.
  1. backup_user(user_id) -> int | null
  • Store a snapshot of the current set of files owned by user_id (name -> size).
  • Return the number of files included in this backup.
  • Return null if the user doesn’t exist.
  1. restore_user(user_id) -> int | null
  • Restore user_id’s files from the most recent backup for that user.
  • First, remove all files currently owned by user_id.
  • Then, for each file in the backup, restore it only if its name is not currently used by some other user.
  • Return the number of files actually restored.
  • If the user exists but has no backup, return 0.
  • If the user doesn’t exist, return null.

Notes

  • Assume up to ~1e5 operations.
  • You may choose any internal data structures, but must satisfy the behavior above.