Implement an in-memory file system
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to design and implement in-memory file system data structures and APIs, covering hierarchical directory modeling, path parsing, file content management, and efficient mutable state handling.
Constraints
- All paths are absolute and use '/' as the separator; the root is '/'.
- Directory and file names consist of lowercase letters only.
- ls on a file path returns a single-element list with the file's name.
- ls on a directory returns its direct children sorted lexicographically.
- mkdir creates all missing intermediate directories.
- addContentToFile appends to (does not overwrite) existing content and creates the file if absent.
- The system is in-memory only (no persistence).
Examples
Input: [['ls', '/'], ['mkdir', '/a/b/c'], ['addContentToFile', '/a/b/c/d', 'hello'], ['ls', '/'], ['readContentFromFile', '/a/b/c/d']]
Expected Output: [[], null, null, ['a'], 'hello']
Explanation: ls on the empty root returns []. mkdir creates /a, /a/b, /a/b/c. addContentToFile creates file d under /a/b/c with content 'hello'. ls '/' now shows ['a']. Reading the file returns 'hello'.
Input: [['mkdir', '/goowmfn'], ['ls', '/goowmfn'], ['mkdir', '/z'], ['ls', '/'], ['addContentToFile', '/goowmfn/c', 'shetopcensf'], ['ls', '/goowmfn'], ['readContentFromFile', '/goowmfn/c']]
Expected Output: [null, [], null, ['goowmfn', 'z'], null, ['c'], 'shetopcensf']
Explanation: After creating /goowmfn it has no children so ls is []. After also creating /z, ls '/' is sorted ['goowmfn', 'z']. Adding file c under /goowmfn makes ls '/goowmfn' return ['c'], and reading it returns the content. (LeetCode 588 sample.)
Input: [['addContentToFile', '/file', 'abc'], ['addContentToFile', '/file', 'def'], ['readContentFromFile', '/file'], ['ls', '/file']]
Expected Output: [null, null, 'abcdef', ['file']]
Explanation: addContentToFile appends rather than overwrites, so two adds yield 'abcdef'. ls on a file path returns just the file name ['file'].
Input: [['ls', '/']]
Expected Output: [[]]
Explanation: Edge case: ls on a brand-new empty root returns an empty list.
Input: [['mkdir', '/b'], ['mkdir', '/a'], ['mkdir', '/c'], ['ls', '/']]
Expected Output: [null, null, null, ['a', 'b', 'c']]
Explanation: Directories are created out of order (b, a, c) but ls returns them sorted lexicographically: ['a', 'b', 'c'].
Hints
- Model the file system as a trie (tree) where each node is either a directory or a file. A directory node holds a map from child name to child node.
- Use a single Node type with a `children` map, an `is_file` flag, and a `content` string. A directory simply has `is_file = False` and may have children; a file has `is_file = True` and accumulates content.
- Split an absolute path on '/' (treating '/' itself as the empty path / root) and walk the trie one component at a time, creating nodes on demand for mkdir and addContentToFile.
- For ls, after walking to the target node: if it is a file, return `[last_component]`; otherwise return the sorted list of its children's names.