Member-only story
Designing an In-Memory File System — Dive into an Advanced Data Structure Problem
In this post, we will walk through a captivating problem from LeetCode: “588. Design In-Memory File System”.
4 min readSep 7, 2023
📖 Problem Statement
We need to design an in-memory file system that can perform operations like creating folders, adding content to files, and retrieving content from files.
🔧 Intuitive Approach
When we think of a file system, a hierarchical structure like a tree comes to mind. For this problem, we can visualize the system as a Trie (a tree-like data structure). Each path component represents a Trie node, and each node can be a file or a directory.
📂 File System Structure
- Root directory exists at the inception.
- Each directory contains files and directories.
- Files can only contain content.
🛠 Optimal Approach and Design
- Data Structure Choice: Use a Trie-like structure, where each Node represents a file or a directory.
- Each Node Contains:
- A flag indicating if it’s a file.
- Content of the file (if it’s a file).
- A HashMap to store child nodes (both files and directories). - Methods to Implement:
ls
: List the contents of the given path.mkdir
: Create a new…