Design an in-memory file system with limits
Company: Harvey
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates the ability to design and implement hierarchical in-memory data structures, manage capacity constraints and OS-style duplicate naming, and reason about edge cases and time/space complexity.
Read the full Harvey Software Engineer interview experience this question came from
Constraints
- 1 <= len(operations) <= 10^4
- 1 <= len(path) <= 200 for every operation path
- Each directory can store at most 5 direct entries
- Path segments are non-empty, case-sensitive strings that cannot contain `/`, and segments `.` and `..` are invalid
Examples
Input: [('get', '/')]
Expected Output: [[]]
Explanation: The filesystem starts empty, so the root directory has no children.
Input: [('addFile', '/path/to/somewhere/file.txt'), ('get', '/'), ('get', '/path'), ('get', '/path/to'), ('get', '/path/to/somewhere')]
Expected Output: ['/path/to/somewhere/file.txt', ['path'], ['to'], ['somewhere'], ['file.txt']]
Explanation: Intermediate directories are created automatically. Each `get` returns the immediate children of that directory.
Hints
- A tree/trie is a natural fit: each directory node can store a hash map of `name -> child node`.
- Handle duplicate file names only in the parent directory of the leaf. Split the leaf into `base + extension`, then try suffixes `(1)`, `(2)`, ... until you find a free name.