Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Design a File System

Design a file system that supports creating directories and files, reading file content, and managing hierarchical directory structures.

Design a file system that supports creating directories and files, reading file content, and managing hierarchical directory structures. The system should handle path navigation, prevent duplicate entries, and efficiently manage file and directory operations.

In this problem, you’ll build an in-memory representation of a Unix-like file system, complete with path resolution, recursive directory management, and uniform handling of entries.


Design an in-memory file system that allows users to perform standard operations like creating, deleting, and reading files and directories across a hierarchical structure.

Functional Requirements:

  • Directory Management: Create directories at any valid path.
  • File Operations: Create files with content, including automatic creation of parent directories.
  • Content Retrieval: Read the content of any existing file via its path.
  • Entry Deletion: Support deleting both individual files and entire directory trees.
  • Conflict Prevention: Prevent creating a file where a directory exists (and vice versa) at the same path.
  • Discovery: Support listing all entries (files and subdirectories) in a given directory.
  • Path Navigation: Navigate the hierarchy using Unix-style absolute paths (e.g., /home/user/file.txt).

Non-Functional Requirements:

  • Uniform Treatment: Use the Composite Pattern to treat files and directories uniformly.
  • Efficiency: Minimize traversal overhead during path resolution and navigation.
  • Thread Safety: Handle concurrent modifications from multiple users without data corruption.
  • Extensibility: Modular design to easily add permissions, metadata, or symbolic links later.
  • Graceful Failures: Handle invalid paths, empty paths, or non-existent entries with clear feedback.

The system is built as a tree where the FileSystem class manages the Root directory.

Diagram
classDiagram
    class FileSystemEntry {
        <<abstract>>
        -String name
        -DateTime created
        +getName()
        +getSize()*
        +delete()*
    }
    
    class File {
        -String content
        +read()
        +write(data)
    }
    
    class Directory {
        -List~FileSystemEntry~ children
        +addEntry(entry)
        +findEntry(name)
        +list()
    }

    class FileSystem {
        -Directory root
        +createFile(path)
        +mkdir(path)
        -resolvePath(path)
    }

    FileSystemEntry <|-- File
    FileSystemEntry <|-- Directory
    Directory o-- FileSystemEntry
    FileSystem --> Directory

Diagram

If your Directory class has separate lists for files and subdirectories, your navigation and search logic will be duplicated and messy.

Solution: Use the Composite Pattern. Create a FileSystemEntry base class. Both File and Directory inherit from it. A Directory simply stores a list of FileSystemEntry objects, allowing it to hold both files and other folders seamlessly.

Resolving a path like /home/user/docs/resume.pdf requires traversing the tree level by level.

Solution: Implement a private resolvePath helper. It splits the string by / and iteratively calls findEntry on the current directory until it reaches the target or fails.

In a multi-user system, one user might delete a directory while another is trying to create a file inside it.

Solution: Use Fine-Grained Locking. Instead of locking the whole file system, use a ReadWriteLock for each Directory. This allows multiple users to read from different parts of the tree simultaneously while ensuring exclusive access for writes.


By solving this problem, you’ll master:

  • Composite Pattern - Managing part-whole hierarchies.
  • Recursive Algorithms - Navigating tree structures.
  • String Processing - Robust path parsing and validation.
  • OOP Principles - Encapsulation and abstraction of system resources.

Ready to see the full implementation? Open the interactive playground to access:

  • 🎯 Step-by-step guidance through the 8-step LLD approach
  • 📊 Interactive UML builder to visualize your design
  • 💻 Complete Code Solutions in Python, Java, C++, TypeScript, JavaScript, C#
  • 🤖 AI-powered review of your design and code

After mastering File System, try these similar problems: