Design an in-memory cloud storage system with file ownership and per-user capacity limits
Company: Dropbox
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an in-memory cloud storage system that manages files with names, sizes and optional owners. File names are unique across the system.
The system must support adding files (with or without an owner), copying files, retrieving file information, removing files and transferring ownership between users.
Each user may have a capacity limit set on them. When a capacity limit is active, the total size of all files owned by that user must not exceed the limit. Operations that would cause a user to exceed their capacity must be rejected.
Implement a class `CloudStorage` with these methods:
- `addFile(name: str, size: int) -> str`: add a file with no owner. Returns a formatted string like "name(size)" on success, or "false" if the file already exists.
- `addFileBy(name: str, size: int, owner: str) -> str`: add a file with the given owner. Returns "name(size)" on success, or "false" if the file exists or the owner's capacity would be exceeded.
- `getFile(name: str) -> str`: return the file info as "name(size)" or "name(size, owner)". Returns an empty string if the file is not found.
- `copyFile(source: str, dest: str) -> str`: copy a file from `source` to `dest`, keeping the owner. Returns the formatted string of the new file on success, or "false" on failure.
- `removeFile(name: str) -> str`: remove a file and free the owner's usage. Returns the formatted string of the removed file on success, or an empty string if it is not found.
- `changeOwnership(name: str, newOwner: str) -> str`: change a file's owner. Returns the formatted string on success, or "false" if the file does not exist or the new owner's capacity would be exceeded.
- `setCapacity(owner: str, capacity: int) -> str`: set a capacity limit for a user. Returns "true" if the user's current usage is within the limit, "false" otherwise.
- `getCapacity(owner: str) -> str`: return the user's remaining capacity as a string, or "unlimited" if no limit is set.
Here is one sequence of calls with the expected results:
```text
addFile("notes.txt", 100) -> "notes.txt(100)"
addFileBy("photo.jpg", 400, "alice") -> "photo.jpg(400)"
getFile("photo.jpg") -> "photo.jpg(400, alice)"
getFile("notes.txt") -> "notes.txt(100)"
setCapacity("alice", 1000) -> "true"
getCapacity("alice") -> "600"
copyFile("photo.jpg", "photo2.jpg") -> "photo2.jpg(400, alice)"
getCapacity("alice") -> "200"
copyFile("photo.jpg", "photo3.jpg") -> "false"
changeOwnership("notes.txt", "alice") -> "notes.txt(100, alice)"
getCapacity("alice") -> "100"
addFileBy("video.mp4", 200, "alice") -> "false"
addFileBy("video.mp4", 200, "bob") -> "video.mp4(200)"
getCapacity("bob") -> "unlimited"
setCapacity("bob", 150) -> "false"
getCapacity("bob") -> "unlimited"
removeFile("photo2.jpg") -> "photo2.jpg(400, alice)"
getCapacity("alice") -> "500"
changeOwnership("photo.jpg", "bob") -> "photo.jpg(400, bob)"
getCapacity("alice") -> "900"
removeFile("photo2.jpg") -> ""
getFile("missing.txt") -> ""
addFile("notes.txt", 50) -> "false"
```
Alice owns 400 bytes when her limit of 1000 is set, so 600 remain. The first copy brings her to 800; a second copy would need 1200 and is rejected. Taking over the unowned `notes.txt` brings her to 900, so a 200-byte file no longer fits. Bob has no limit, and `setCapacity("bob", 150)` fails because he already owns 200 bytes, so he stays unlimited. Removing `photo2.jpg` and handing `photo.jpg` to Bob leave Alice with only `notes.txt`, which is 100 of her 1000.
```hint What every check needs
Find the one number per user that every capacity check reads, and keep it current on each operation so no check has to scan all files.
```
```hint Ownership edge cases
Walk through `changeOwnership` when the file has no owner yet, and when the new owner already owns the file.
```
### Constraints & Assumptions
- File names have 1 to 20 characters and use letters, digits and dots.
- User names use letters and digits.
- `1 <= size <= 10000`
- A capacity, when set, is a positive integer.
- At most 1000 calls in total.
- A call to `addFile` or `addFileBy` with a name that already exists can occur and must be rejected.
### Clarifying Questions to Ask
- Does a copy count against the owner's capacity even though its bytes match the source?
- Should `changeOwnership` to the file's current owner succeed, and does it need a capacity check?
- Is an operation that brings a user exactly to the limit allowed?
- If `setCapacity` returns "false", does the user's previous limit stay in place?
- Should `addFileBy` include the owner in its return string the way `getFile` does?
### What a Strong Answer Covers
- A data model with files keyed by name (size and optional owner), per-user limits and per-user usage, and a reason for storing usage over recomputing it.
- A capacity check on every path that adds bytes to a user (`addFileBy`, `copyFile`, `changeOwnership`), done before any state changes.
- Usage bookkeeping on remove and transfer, including unowned files and a transfer to the same owner.
- The exact return value of each method: both file formats, "false", the empty string, "true" and "unlimited".
- The time and space cost of each operation.
### Follow-up Questions
- How would you add deduplication so a copy shares storage with its source, and how would capacity be charged then?
- How would you make the store safe for concurrent calls from many users?
- How would you add an expiry time to files, and how would expired files give back capacity?
- How would you list a user's largest files, or files by name prefix, without scanning the whole store?
Overview: Build an in-memory cloud storage class that adds, copies, removes and transfers files while enforcing optional per-user capacity limits. It tests per-owner usage bookkeeping with hash maps, checking limits before changing state, and returning the exact status string for each operation.