Implement a hierarchical file store
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in modeling hierarchical file systems, path parsing and manipulation, managing mutable file contents, and analyzing algorithmic time and space complexity within the Coding & Algorithms domain.
Constraints
- 1 <= len(operations) <= 20000
- len(operations) == len(arguments)
- The sum of all path lengths is at most 200000
- The sum of all appended content lengths is at most 200000
- All paths are valid absolute paths
- All `list` and `readFile` operations refer to existing paths
- `readFile` is only called on file paths
Examples
Input: (['makeDirectory', 'writeFile', 'list', 'list', 'readFile', 'writeFile', 'readFile'], [['/a/b'], ['/a/b/c.txt', 'hello'], ['/'], ['/a/b/c.txt'], ['/a/b/c.txt'], ['/a/b/c.txt', 'world'], ['/a/b/c.txt']])
Expected Output: [['a'], ['c.txt'], ['hello'], ['helloworld']]
Explanation: Creates `/a/b`, writes to `c.txt`, lists the root and the file path, then reads before and after appending more content.
Input: (['list'], [['/']])
Expected Output: [[]]
Explanation: Edge case: the file store is empty except for the root directory, so listing `/` returns an empty list.
Input: (['makeDirectory', 'writeFile', 'writeFile', 'makeDirectory', 'list', 'readFile', 'list'], [['/docs'], ['/docs/z.txt', 'z'], ['/docs/a.txt', 'a'], ['/docs/sub_dir'], ['/docs'], ['/docs/a.txt'], ['/docs/sub_dir']])
Expected Output: [['a.txt', 'sub_dir', 'z.txt'], ['a'], []]
Explanation: Children of `/docs` must be returned in lexicographic order, and listing an empty directory returns an empty list.
Input: (['writeFile', 'list', 'list', 'list', 'readFile'], [['/logs/2024.01_run_1.txt', 'x'], ['/'], ['/logs'], ['/logs/2024.01_run_1.txt'], ['/logs/2024.01_run_1.txt']])
Expected Output: [['logs'], ['2024.01_run_1.txt'], ['2024.01_run_1.txt'], ['x']]
Explanation: Writing a file should automatically create missing parent directories. Listing a file path returns only that file name.
Input: (['writeFile', 'makeDirectory', 'writeFile', 'list', 'readFile'], [['/b.txt', 'one'], ['/a'], ['/b.txt', 'two'], ['/'], ['/b.txt']])
Expected Output: [['a', 'b.txt'], ['onetwo']]
Explanation: The root can contain both directories and files. Names must be sorted together, and repeated writes append content.
Hints
- Model the file store as a tree (trie) where each directory maps child names to child nodes.
- Use a hash map for children so creation and lookup are fast, and only sort child names when answering a `list` query.