Design an In-Memory File System

Quick Overview

Implement the cited in-memory hierarchical file system with path listing, directory creation, file-content append, and file-content read operations.

Design an In-Memory File System

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Design an In-Memory File System Implement `run_file_system(operations: list[list[str]]) -> list[str]` for an initially empty in-memory hierarchy whose root is `/`. Supported operations are: - `["mkdir", path]`: create all missing directories on `path`. - `["add", file_path, content]`: append `content` to an existing file, or create the file in an existing parent directory. - `["read", file_path]`: return the file's complete content. - `["ls", path]`: if `path` is a file, return its name; if it is a directory, return its direct child names in lexicographic order, joined by commas. Return the strings produced by `read` and `ls`, in operation order. Mutating operations produce no result entry. ## Valid Input Domain - Paths are absolute and canonical, with lowercase alphanumeric names separated by `/`. - Names contain no commas. A file and directory never share a path. - Every `read` target and every `ls` target exists. The parent of an `add` target exists. ## Constraints - `0 <= operations.length <= 100,000` - Total path and content length is at most 1,000,000 characters. ## Public Examples ### Example 1 Input: `[["mkdir", "/a/b"], ["add", "/a/b/f", "hi"], ["ls", "/a"], ["read", "/a/b/f"]]` Output: `["b", "hi"]` ### Example 2 Input: `[["add", "/x", "ab"], ["add", "/x", "cd"], ["ls", "/x"], ["read", "/x"]]` Output: `["x", "abcd"]` ```hint Separate nodes by kind Choose a representation that distinguishes directories from files while sharing path traversal logic. ```

Quick Answer: Implement the cited in-memory hierarchical file system with path listing, directory creation, file-content append, and file-content read operations.

|Home/Coding & Algorithms/Snowflake
Snowflake logo
Snowflake
Aug 24, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
4
0

Design an In-Memory File System

Implement run_file_system(operations: list[list[str]]) -> list[str] for an initially empty in-memory hierarchy whose root is /.

Supported operations are:

  • ["mkdir", path] : create all missing directories on path .
  • ["add", file_path, content] : append content to an existing file, or create the file in an existing parent directory.
  • ["read", file_path] : return the file's complete content.
  • ["ls", path] : if path is a file, return its name; if it is a directory, return its direct child names in lexicographic order, joined by commas.

Return the strings produced by read and ls, in operation order. Mutating operations produce no result entry.

Valid Input Domain

  • Paths are absolute and canonical, with lowercase alphanumeric names separated by / .
  • Names contain no commas. A file and directory never share a path.
  • Every read target and every ls target exists. The parent of an add target exists.

Constraints

  • 0 <= operations.length <= 100,000
  • Total path and content length is at most 1,000,000 characters.

Public Examples

Example 1

Input: [["mkdir", "/a/b"], ["add", "/a/b/f", "hi"], ["ls", "/a"], ["read", "/a/b/f"]]

Output: ["b", "hi"]

Example 2

Input: [["add", "/x", "ab"], ["add", "/x", "cd"], ["ls", "/x"], ["read", "/x"]]

Output: ["x", "abcd"]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...