Design an In-Memory Cloud Storage Service with Users, Backups, and Restore
AirbnbSoftware Engineer · Online AssessmentLast updated
Implement an in-memory cloud storage system supporting user storage limits, globally unique files, ranked file queries, account merges, and transactional backup and restore operations.
Problem Statement
Design an in-memory cloud storage service that manages users and their files with strict quota enforcement, account merging, and snapshot-based backup and restore capabilities.
Implement the CloudStorage class with the following methods:
-
add_user(user_id: str, capacity: int) -> bool: Registers a new user with the given storagecapacity(in bytes). ReturnsTrueon success. ReturnsFalseifuser_idalready exists or ifcapacity bool: Uploads a file withfile_nameandfile_sizeforuser_id. File names are globally unique across all users. ReturnsTrueif successfully added. ReturnsFalseifuser_iddoes not exist,file_size list[str]: Returns a list of up tokfile names owned byuser_id. Files must be ordered descending byfile_size. If sizes are equal, break ties alphabetically (ascending byfile_name). Ifuser_iddoes not exist or has no files, return an empty list. -
merge_accounts(target_user_id: str, source_user_id: str) -> bool: Mergessource_user_idintotarget_user_id:target_user_id's capacity increases bysource_user_id's capacity.- Ownership of all files belonging to
source_user_idtransfers totarget_user_id. source_user_idand any backups belonging to it are deleted.- Returns
Falseiftarget_user_id == source_user_idor if either user does not exist. Otherwise, returnsTrue.
-
backup_user(user_id: str) -> bool: Saves a snapshot of the files currently owned byuser_id(capturing file names and sizes). Overwrites any existing backup for this user. ReturnsFalseifuser_iddoes not exist; otherwise returnsTrue. -
restore_user(user_id: str) -> bool: Restores the user's files from their latest backup. This operation must be transactional (all-or-nothing):- Returns
Falseifuser_iddoes not exist or has no backup. - Returns
Falseif any file in the backup is currently owned by another user. - Returns
Falseif the total size of files in the backup exceedsuser_id's current capacity. - If valid, removes all files currently owned by
user_id, restores the backup files underuser_id, and returnsTrue. If invalid, the system state remains completely unchanged.
- Returns
Example
storage = CloudStorage()
storage.add_user("alice", 100) # True
storage.add_user("bob", 50) # True
storage.add_file("alice", "a.txt", 40) # True (alice used: 40/100)
storage.add_file("alice", "b.txt", 30) # True (alice used: 70/100)
storage.add_file("alice", "c.txt", 40) # False (70 + 40 > 100)
storage.backup_user("alice") # True (snapshot: a.txt: 40, b.txt: 30)
storage.add_file("alice", "d.txt", 20) # True (alice used: 90/100)
storage.get_largest_files("alice", 2) # ["a.txt", "b.txt"]
storage.restore_user("alice") # True (d.txt is removed; a.txt and b.txt restored)
storage.get_largest_files("alice", 5) # ["a.txt", "b.txt"]
Constraints
user_idandfile_nameare non-empty strings of alphanumeric characters.1 = 1- Total number of method calls
<= 5 * 10^4.
Hints
Hint 1
Maintain a global file registry to check file existence and ownership in O(1) time.
Hint 2
For `get_largest_files`, maintain files per user in a map or set, and sort on query or keep a balanced BST / sorted list per user.
Hint 3
For transactional `restore_user`, validate all preconditions (no name conflicts with other users, capacity limits) before mutating any user or global state.
Follow-up questions
- How would you optimize `get_largest_files` if reads are much more frequent than file additions or deletions?
- How would you support concurrent operations across different users using fine-grained locking without causing deadlocks during `merge_accounts`?
- If backups were versioned by timestamp rather than overwriting the previous snapshot, how would you design the retention and garbage collection policy?
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.