Design a File System
What is the File System Problem?
Section titled “What is the File System Problem?”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.
Problem Overview
Section titled “Problem Overview”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.
Core Requirements
Section titled “Core Requirements”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.
What’s Expected?
Section titled “What’s Expected?”1. System Architecture
Section titled “1. System Architecture”The system is built as a tree where the FileSystem class manages the Root directory.
2. Key Classes to Design
Section titled “2. Key Classes to Design”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
System Flow
Section titled “System Flow”Path Resolution Flow (mkdir /a/b)
Section titled “Path Resolution Flow (mkdir /a/b)”Key Design Challenges
Section titled “Key Design Challenges”1. Uniform Treatment of Files and Folders
Section titled “1. Uniform Treatment of Files and Folders”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.
2. Path Navigation
Section titled “2. Path Navigation”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.
3. Thread Safety
Section titled “3. Thread Safety”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.
What You’ll Learn
Section titled “What You’ll Learn”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.
View Complete Solution & Practice
Section titled “View Complete Solution & Practice”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
Related Problems
Section titled “Related Problems”After mastering File System, try these similar problems:
- JSON Parser - Another hierarchical data challenge.
- Library Management - Categorization and tracking.
- LRU Cache - Advanced data structure management.