PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • hard
  • Snowflake
  • Coding & Algorithms
  • Software Engineer

Implement an in-memory file system

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Implement an **in-memory file system** supporting basic directory and file operations. You must support the following methods (names are illustrative; any equivalent interface is fine): 1. `ls(path)` - If `path` is a file path, return a list containing **only the file name**. - If `path` is a directory path, return the names of **files and subdirectories directly under it**, sorted lexicographically. 2. `mkdir(path)` - Create a directory at the given absolute path. - Create any missing intermediate directories. 3. `addContentToFile(filePath, content)` - If the file does not exist, create it. - Append `content` to the existing file content. 4. `readContentFromFile(filePath)` - Return the full content of the file. ### Notes / Constraints - Paths are absolute and use `/` as the separator. - The root directory is `/`. - Directory and file names consist of lowercase letters (you may assume no invalid characters). - The system is in-memory only (no persistence). - Aim for reasonable time complexity per operation.

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.

Implement an in-memory file system supporting basic directory and file operations. All paths are absolute, use `/` as the separator, and the root directory is `/`. Names consist of lowercase letters only. You must support four operations: 1. `ls(path)` - If `path` is a file path, return a list containing only the file name. If `path` is a directory path, return the names of files and subdirectories directly under it, sorted lexicographically. 2. `mkdir(path)` - Create a directory at the given absolute path, creating any missing intermediate directories. 3. `addContentToFile(filePath, content)` - If the file does not exist, create it; then append `content` to the file's existing content. 4. `readContentFromFile(filePath)` - Return the full content of the file. **Harness contract:** Implement a single function/method `solution(operations)` (named `solution` in all four languages) that takes a list of operations and returns a list of results. Each operation is a list whose first element is the op name (`"ls"`, `"mkdir"`, `"addContentToFile"`, `"readContentFromFile"`) followed by its string arguments. For each operation, append its result to the output list: `ls` appends a list of strings, `readContentFromFile` appends a string, and `mkdir`/`addContentToFile` append `null` (None). **Example:** ``` operations = [["ls", "/"], ["mkdir", "/a/b/c"], ["addContentToFile", "/a/b/c/d", "hello"], ["ls", "/"], ["readContentFromFile", "/a/b/c/d"]] returns [[], null, null, ["a"], "hello"] ```

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a JSON Parser - Snowflake (hard)
  • Solve Matrix and Array Distance Problems - Snowflake (medium)
  • Solve Array Distance and Wiki Navigation - Snowflake (medium)
  • Implement Document Predicate APIs - Snowflake (medium)
  • Find Shortest Wiki Click Path - Snowflake (medium)