Implement an In-Memory File System with Listing, Path Lookup and Deletion

Quick Overview

Implement an in-memory file system class over absolute Unix-style paths that lists entries, creates directories, appends to and reads files, and also supports path lookup and deletion. It tests tree modelling with name-keyed children, shared path resolution, clear edge-case semantics and per-operation cost.

Implement an In-Memory File System with Listing, Path Lookup and Deletion

Company: Snowflake

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

Implement an in-memory file system as a class, coding it live. Paths are absolute and Unix-style, such as `/a/b/c`, and the root is `/`. The task resembles a classic exercise whose baseline has four operations: - `ls(path)`: if `path` is a file, return a list containing only the file's name; if it is a directory, return the names of the files and directories directly inside it, in lexicographic order. - `mkdir(path)`: create the directory at `path`, creating any missing intermediate directories. - `add_content_to_file(file_path, content)`: create the file with `content` if it does not exist, otherwise append `content` to it. - `read_content_from_file(file_path)`: return the file's whole content. This version asks for more than the baseline: the file system must also support querying a path and deleting files and directories. The report does not give exact signatures or semantics for the extra operations, so agree on them with the interviewer before coding. ```hint One node per name Think about what a single name in a path should map to, and how a directory finds the entry for the next name without scanning all of its entries. ``` ```hint Share the walk Almost every operation starts by walking a path. Decide once what that walk does when a name is missing or turns out to be a file, and reuse it everywhere. ``` ```hint Removing an entry To make a file or a whole directory disappear, ask which node's state actually has to change. ``` ### Constraints and Clarifications - The round started directly with coding, so the expectation is runnable code for the class, not only a design discussion. - Names are non-empty and never contain `/`. File contents are strings. ### Clarifying Questions - What should a path query return: whether the path exists, whether it is a file or a directory, a file's size or content, or a directory's listing? - When a directory that is not empty is deleted, should its whole subtree go, or should the call fail? May the root be deleted? - Should `add_content_to_file` create missing parent directories, or fail when the parent does not exist? - What should happen when a name in the middle of a path is a file, or when `mkdir` names an existing file? - How should errors be reported: exceptions, a boolean result, or an empty result? - Can paths contain a trailing slash, repeated slashes, or `.` and `..` components? ### What a Strong Answer Covers - A tree of nodes, one per path component, with child lookup by name in constant expected time and a clear distinction between files and directories - One path-resolution helper shared by every operation, with defined behavior for missing names and for a file in the middle of a path - Correct semantics for the baseline operations and for the agreed query and delete operations, including the root, nested paths and non-empty directories - The cost of each operation in terms of path length and the number of entries listed - Working code delivered in a sensible order: the baseline first, then the extra operations ### Follow-up Questions - Add a move or rename operation. How do you stop a directory from being moved into its own subtree? - `ls` on a directory with many entries is called far more often than the directory changes. How do you avoid sorting on every call? - How would you make the class safe for concurrent callers without one global lock? - How would you report the total size of every directory and keep it correct through appends and deletions?

Overview: Implement an in-memory file system class over absolute Unix-style paths that lists entries, creates directories, appends to and reads files, and also supports path lookup and deletion. It tests tree modelling with name-keyed children, shared path resolution, clear edge-case semantics and per-operation cost.

Read the full Snowflake Software Engineer interview experience this question came from

Snowflake logo
Snowflake
May 7, 2026
hardSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

Implement an in-memory file system as a class, coding it live. Paths are absolute and Unix-style, such as /a/b/c, and the root is /. The task resembles a classic exercise whose baseline has four operations:

  • ls(path) : if path is a file, return a list containing only the file's name; if it is a directory, return the names of the files and directories directly inside it, in lexicographic order.
  • mkdir(path) : create the directory at path , creating any missing intermediate directories.
  • add_content_to_file(file_path, content) : create the file with content if it does not exist, otherwise append content to it.
  • read_content_from_file(file_path) : return the file's whole content.

This version asks for more than the baseline: the file system must also support querying a path and deleting files and directories. The report does not give exact signatures or semantics for the extra operations, so agree on them with the interviewer before coding.

Constraints and Clarifications

  • The round started directly with coding, so the expectation is runnable code for the class, not only a design discussion.
  • Names are non-empty and never contain / . File contents are strings.

Clarifying Questions Guidance

  • What should a path query return: whether the path exists, whether it is a file or a directory, a file's size or content, or a directory's listing?
  • When a directory that is not empty is deleted, should its whole subtree go, or should the call fail? May the root be deleted?
  • Should add_content_to_file create missing parent directories, or fail when the parent does not exist?
  • What should happen when a name in the middle of a path is a file, or when mkdir names an existing file?
  • How should errors be reported: exceptions, a boolean result, or an empty result?
  • Can paths contain a trailing slash, repeated slashes, or . and .. components?

What a Strong Answer Covers Guidance

  • A tree of nodes, one per path component, with child lookup by name in constant expected time and a clear distinction between files and directories
  • One path-resolution helper shared by every operation, with defined behavior for missing names and for a file in the middle of a path
  • Correct semantics for the baseline operations and for the agreed query and delete operations, including the root, nested paths and non-empty directories
  • The cost of each operation in terms of path length and the number of entries listed
  • Working code delivered in a sensible order: the baseline first, then the extra operations

Follow-up Questions Guidance

  • Add a move or rename operation. How do you stop a directory from being moved into its own subtree?
  • ls on a directory with many entries is called far more often than the directory changes. How do you avoid sorting on every call?
  • How would you make the class safe for concurrent callers without one global lock?
  • How would you report the total size of every directory and keep it correct through appends and deletions?