Design a file storage query system with add, delete and timestamp range queries
Company: Microsoft
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a file storage query system that supports adding files with associated timestamps, deleting files, and efficiently querying all files whose timestamps fall within a given inclusive time range.
Each file has a unique `name` (a string) and an integer `timestamp`. The system must support efficient range queries that return all file names with timestamps in `[startTime, endTime]`, sorted lexicographically.
If a file with the same name already exists when adding, the operation should fail. Deleting a non-existent file should also indicate failure.
Implement the following methods:
- `addFile(name: str, timestamp: int) -> bool`: Add a file with the given name and timestamp. Returns True if the file was successfully added, or False if a file with that name already exists.
- `deleteFile(name: str) -> bool`: Delete the file with the given name. Returns True if the file was found and deleted, or False if no file with that name exists.
- `queryRange(startTime: int, endTime: int) -> List[str]`: Return a lexicographically sorted list of file names whose timestamps fall within the inclusive range `[startTime, endTime]`.
- `getFileTimestamp(name: str) -> int`: Return the timestamp of the file with the given name, or -1 if the file does not exist.
- `fileCount() -> int`: Return the total number of files currently stored in the system.
Example:
```text
fs = FileStorageQuerySystem()
fs.addFile("report.pdf", 100) -> True
fs.addFile("notes.txt", 200) -> True
fs.addFile("data.csv", 150) -> True
fs.addFile("a_log.txt", 150) -> True
fs.addFile("report.pdf", 300) -> False
fs.queryRange(100, 200) -> ["a_log.txt", "data.csv", "notes.txt", "report.pdf"]
fs.deleteFile("notes.txt") -> True
fs.deleteFile("notes.txt") -> False
fs.queryRange(150, 300) -> ["a_log.txt", "data.csv"]
fs.getFileTimestamp("report.pdf") -> 100
fs.getFileTimestamp("notes.txt") -> -1
fs.fileCount() -> 3
```
The second add of `report.pdf` fails because the name is taken, so the file keeps timestamp 100. The first query includes both endpoints (100 and 200) and lists the names in string order, which differs from their timestamp order. After `notes.txt` is deleted, deleting it again fails, and the query over `[150, 300]` returns only the two files stamped 150.
```hint Two kinds of lookup
Some methods look a file up by name and one looks files up by a timestamp range. Check whether a single structure can answer both quickly.
```
```hint Two orders
The range is defined on timestamps, but the output is ordered by name. Decide which order the stored data follows and where the other ordering gets applied.
```
### Constraints & Assumptions
- `1 <= timestamp <= 10^9`
- File names are non-empty strings made of letters, digits, `.` and `_`.
- At most `10^4` total calls will be made across all methods.
- For `queryRange`, `startTime <= endTime`.
### Clarifying Questions to Ask
- Is lexicographic order plain case-sensitive string comparison (so `B.txt` sorts before `a.txt`), or should it ignore case or sort numbers naturally?
- Can a name be added again after it has been deleted?
- What is the mix of calls: are range queries much more frequent than writes, and how many names can a single query return?
### What a Strong Answer Covers
- How each method finds what it needs: a lookup by name for some, files by timestamp range for the query.
- Keeping every structure consistent across successful adds, deletes and failed calls.
- Inclusive boundaries at both ends of the range, including several files that share one timestamp.
- Output sorted by name, separate from the order used to find the range.
- Time and space complexity for each method, and what changes if writes or result sizes grow.
### Follow-up Questions
- How would you let a file's timestamp be updated in place?
- When a range query matches millions of files, how would you return the names a page at a time in name order?
- If writes far outnumber queries at a much larger scale, which structure would you replace, and what would each method cost then?
- How would you make the system safe for concurrent readers and writers?
Overview: Design an in-memory file store where each file has a unique name and an integer timestamp, with add, delete, lookup, count and inclusive time range queries that return names in sorted order. It tests choosing structures for name lookups and range queries, handling range boundaries, and keeping indexes consistent on delete.