Design a file collections tracker with total size and top K collections by size
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a system that tracks files and their membership in collections. Each file has a unique name, a size (in bytes), and can belong to zero or more collections.
The system should support adding files with their sizes and collection memberships, querying the total size of all unique files in the system, and retrieving the top K collections ranked by the total size of their member files.
If a file that already exists is added again, the call should be ignored. If two collections have the same total file size, they should be ordered lexicographically by collection name.
Implement the following methods:
- `addFile(fileName: str, fileSize: int, collectionNames: List[str])`: Add a file with the given name, size, and list of collection memberships. If the file already exists, this call is a no-op.
- `getTotalSize() -> int`: Return the total size of all unique files currently tracked in the system.
- `getTopKCollections(k: int) -> List[str]`: Return the names of the top K collections ranked by total file size in descending order. Ties are broken lexicographically by collection name in ascending order.
Example:
```text
Input:
["FileCollectionsTracker", "addFile", "addFile", "getTotalSize", "addFile", "getTotalSize", "getTopKCollections"]
[[], ["a.txt", 500, ["alpha", "beta"]], ["a.txt", 999, ["gamma"]], [], ["b.txt", 300, ["beta"]], [], [2]]
Output: [null, null, null, 500, null, 800, ["beta", "alpha"]]
```
```python
tracker = FileCollectionsTracker()
tracker.addFile("a.txt", 500, ["alpha", "beta"]) # a.txt joins alpha and beta
tracker.addFile("a.txt", 999, ["gamma"]) # duplicate name, ignored
tracker.getTotalSize() # 500 (a.txt counted once)
tracker.addFile("b.txt", 300, ["beta"]) # b.txt joins beta
tracker.getTotalSize() # 800 (500 + 300)
tracker.getTopKCollections(2) # ["beta", "alpha"] (beta = 800, alpha = 500)
```
The second `addFile` call is ignored as a whole, so `a.txt` keeps its size of 500 and the collection `gamma` is never created.
```hint Cost of ranking
Count how much work `getTopKCollections` does if it adds up every member's size on each call. Then ask which numbers could be kept up to date while files are being added.
```
```hint Duplicates
The no-op rule is keyed on the file name. Check what it means for the second call's size and collections, and what should happen if one call lists the same collection twice.
```
### Constraints & Assumptions
- `1 <= fileSize <= 10^9`
- `0 <= collectionNames.length <= 100`
- File names and collection names are non-empty strings.
- File names are unique. Adding a duplicate file is a no-op.
- `1 <= k <= number of distinct collections`
- At most `10^4` total calls will be made.
### Clarifying Questions to Ask
- If `collectionNames` lists the same collection twice, should the file count once toward that collection?
- Does a collection exist only after some file has joined it, or can empty collections be created and ranked?
- Is lexicographic order plain case-sensitive string comparison?
- How often is `getTopKCollections` called compared with `addFile`?
### What a Strong Answer Covers
- The state each method needs and the structures that hold it, including how each collection's total is produced and what that costs per call.
- The duplicate rule: a repeated file name ignores the whole call, including its new size and its collections.
- Ranking by size descending with names ascending on ties, using a sort key that gets both directions right.
- Time and space complexity for each method.
- Edge cases: a file with no collections, a collection listed twice in one call, and totals beyond the 32-bit range.
### Follow-up Questions
- How would you support removing a file or changing its size?
- If `getTopKCollections` is called far more often than `addFile` and there are many collections, how would you make it cheaper?
- Collections can now contain other collections, and a parent's size includes every file in its subtree, each counted once. How does the design change?
- How would you make the tracker safe to use from multiple threads?
Overview: Design an in-memory tracker that records files with their sizes and collection memberships, then reports the total size of unique files and the top K collections by total size. It tests duplicate handling, deciding what state to keep current as files arrive, and sorting with a size-descending, name-ascending tie-break.