Design In-Memory File System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-04-13

Problem Statement

Designing an in-memory file system, similar to how an operating system organizes files and directories, but without using actual disk storage.

The system should simulate basic file system operations such as creating files/folders, reading/writing data, and navigating directories — all stored in memory.


This problem tests your ability to model hierarchical data, handle CRUD operations efficiently, and design class relationships with good abstraction.


🎯 Problem Statement


Design and implement an in-memory file system that supports:


•File and directory creation
•Reading and writing file content
•Listing directory contents
•Navigating through a hierarchical path structure


✅ Functional Requirements


Create Directories

•Support creation of directories using full paths (e.g., /documents/projects/).
•Create intermediate directories if they don’t exist.

Create Files

•Support creating files at any valid directory path (e.g., /documents/notes.txt).

Read and Write to Files

•Write data to files (append or overwrite).
•Read file contents.

List Directory Contents

•Given a directory path, list all files and subdirectories (alphabetically).

Delete

•Support deleting files or empty directories.

Path Traversal

•Handle absolute and relative paths (/root/home/docs or ../docs).

Error Handling

•Throw appropriate errors for invalid paths, permission issues, or non-existent files.


⚙️ Non-Functional Requirements


Aspect with Description


Performance

O(depth) for traversal; O(1) for read/write operations


Scalability

Handle thousands of files/directories in memory


Consistency

Maintain correct file tree structure


Persistence

Not required (data lost on restart)


Extensibility

Easy to add features like permissions, file metadata, or caching


🧱 Core Entities / Classes


1. FileSystem

Main interface providing APIs for all file system operations.

1 2class FileSystem { 3 Directory root; 4 5 public FileSystem(); 6 public List<String> ls(String path); 7 public void mkdir(String path); 8 public void addFile(String filePath, String content); 9 public String readFile(String filePath); 10}


2. Directory

Represents a folder containing subdirectories and files.

1 2class Directory { 3 String name; 4 Map<String, Directory> subDirectories; 5 Map<String, File> files; 6}


3. File

Represents a single file and its content.

1 2class File { 3 String name; 4 StringBuilder content; 5 6 void write(String data); 7 String read(); 8}


4. PathResolver (Utility class)

Handles parsing and navigation of file paths.

1 2class PathResolver { 3 public Directory navigate(String path); 4}


🧠 Key Operations Explained


🟩 mkdir(path)

•Example: mkdir("/a/b/c")
•Creates directory c and intermediate folders if needed.


🟩 addFile(path, content)

•Example: addFile("/a/b/file.txt", "hello")
•Creates file at the given path, writes “hello” to it.


🟩 readFile(path)

•Returns file content for the given path.


🟩 ls(path)

•Lists all files and directories alphabetically at the given path.


🧩 Example Usage

1 2Input: 3mkdir("/music/rock") 4addFile("/music/rock/song.txt", "we will rock you") 5readFile("/music/rock/song.txt") 6ls("/music") 7 8Output: 9"we will rock you" 10["rock"]


📂 Internal Data Structure


The file system forms a tree-like structure:

1 2/ 3├── documents 4│ ├── notes.txt 5│ └── projects 6│ └── report.doc 7├── music 8│ ├── rock 9│ │ └── song.txt 10│ └── pop
•Each node is either a Directory or a File.
•The FileSystem class maintains a root Directory.


💬 Example API Usage

1 2FileSystem fs = new FileSystem(); 3 4fs.mkdir("/documents/projects"); 5fs.addFile("/documents/projects/todo.txt", "1. Design FS\n2. Write Code"); 6fs.addFile("/documents/notes.txt", "Meeting notes..."); 7 8System.out.println(fs.readFile("/documents/projects/todo.txt")); 9// Output: 1. Design FS\n2. Write Code 10 11System.out.println(fs.ls("/documents")); 12// Output: [notes.txt, projects]


🧱 Optional Features (for extension)


Feature with Description


Permissions

Add role-based read/write permissions


Metadata

Store file size, creation date, modified time


Compression

Store large content efficiently


Persistence Layer

Dump the structure to disk or cloud for recovery


Search Service

Index files for fast path or content search


💬 Interview Prompt (Final Form)

Design an In-Memory File System.
The system should allow users to create directories and files, read/write file content, and list directory contents — all stored in memory.
Explain your class design, object relationships, and how you would handle hierarchical paths and scalability.


Solution

Comments