PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

Implement an in-memory file system with absolute and relative path resolution, sorted listing, directory changes, creation, and removal. Enforce deterministic error codes, atomic validation, recursive deletion rules, and protection for the current directory and its ancestors.

  • medium
  • Shopify
  • Coding & Algorithms
  • Software Engineer

Implement an In-Memory File System

Company: Shopify

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Implement an In-Memory File System The source reports an in-memory file-system exercise but not its exact error serialization or current-directory removal policy. The error codes, driver, and busy-directory rule below are deterministic practice assumptions. Implement: ```python class FileSystemError(Exception): code: str class FileSystem: def ls(self, path: str = ".") -> list[str]: ... def cd(self, path: str) -> None: ... def add(self, path: str, is_directory: bool) -> None: ... def remove(self, path: str, recursive: bool = False) -> None: ... def run_file_system(operations: list[list[object]]) -> list[object]: ... ``` The file system starts with an empty root `/` and current working directory `/`. Directories contain named children; files contain no data. ## Paths and Successful Results - Support absolute and relative paths, repeated `/`, `.`, `..`, and trailing separators. Traversal above root stays at root. An empty path string is invalid. - `ls(path)` returns sorted child names for a directory and `[basename]` for a file. - `cd(path)` changes the working directory only when the resolved target is a directory and returns `None`. - `add(path, is_directory)` creates exactly one file or directory. The resolved parent must already be a directory, and the target must not exist. Success returns `None`. - `remove(path, recursive=False)` removes a file or empty directory. A nonempty directory requires `recursive=True`. Success returns `None`. Resolve the complete path before mutating anything. A file may be the final target of `ls` or `remove`, but no operation may traverse through a file. ## Deterministic Errors Class methods raise `FileSystemError` with exactly one of these codes: - `"invalid_path"`: the path is not a nonempty string. - `"not_found"`: a required component or target is absent. - `"not_directory"`: traversal would continue through a file, or `cd` targets a file. - `"already_exists"`: `add` resolves to an existing node. - `"root"`: `remove` targets `/`. - `"busy"`: `remove` targets the current working directory or any of its ancestors, even with `recursive=True`. - `"directory_not_empty"`: a non-busy, non-root directory has children and `recursive=False`. For `remove`, resolve first, then apply error precedence `root`, `busy`, and `directory_not_empty`. Resolution errors occur before those checks. Every failed class operation is atomic: the tree and current working directory remain unchanged. The driver uses these commands: - `["LS"]` or `["LS", path]` - `["CD", path]` - `["ADD", path, is_directory]` - `["REMOVE", path]` or `["REMOVE", path, recursive]` It returns one result per operation. On a successful command, append the method's result. On `FileSystemError`, append `["ERROR", code]` and continue. A malformed command, unknown command, or non-Boolean flag appends `["ERROR", "invalid_operation"]` without calling a class method. ## Performance and Tests Aim for time proportional to normalized path components plus returned output, except that recursive deletion is proportional to the removed subtree. Test above-root traversal, repeated separators, traversal through files, duplicate adds, failed `cd`, removing root, removing the current directory and each ancestor, recursive removal of an unrelated subtree, failure atomicity, and error precedence.

Quick Answer: Implement an in-memory file system with absolute and relative path resolution, sorted listing, directory changes, creation, and removal. Enforce deterministic error codes, atomic validation, recursive deletion rules, and protection for the current directory and its ancestors.

Run literal LS, CD, ADD, and REMOVE commands against an in-memory file system with absolute and relative paths. Support repeated separators, dot components, parent traversal clamped at root, deterministic error codes, recursive deletion, and a busy-directory rule that protects the current directory and its ancestors.

Constraints

  • The file system begins with an empty root and current directory /.
  • Files contain no data; directories contain named children.
  • ADD creates exactly one node and its resolved parent must already be a directory.
  • REMOVE cannot target root, the current directory, or an ancestor of the current directory.
  • Malformed driver commands return ['ERROR', 'invalid_operation']; class-operation errors use their documented codes.

Examples

Input: ([["LS"]],)

Expected Output: [[]]

Explanation: The root starts empty.

Input: ([["ADD", "/docs", True], ["ADD", "/z.txt", False], ["ADD", "/docs/a.txt", False], ["LS", "/"], ["LS", "/docs/a.txt"]],)

Expected Output: [None, None, None, ["docs", "z.txt"], ["a.txt"]]

Explanation: ADD creates one node and LS sorts directories or names a file.

Hints

  1. Represent each directory with a mapping from child name to node.
  2. Resolve paths component by component so traversal through a file can be rejected.
  3. Compare canonical component lists to detect whether a removal target is an ancestor of cwd.
Last updated: Jul 15, 2026

Loading coding console...

PracHub

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

Product

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

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

  • Grid Robot Command Simulator - Shopify (medium)
  • Terminal-Controlled Grid Rover Simulator - Shopify (medium)
  • Compute Theme Similarity - Shopify (medium)
  • Implement a Constant-Time LRU Cache - Shopify (medium)