Design In-Memory File System - Problem

๐Ÿ—‚๏ธ Design In-Memory File System

Imagine you're building a cloud storage system that needs to simulate a complete file system entirely in memory. You need to create a data structure that can handle all the basic operations you'd expect from a real file system: listing directories, creating folders, writing files, and reading their contents.

Your FileSystem class should support these operations:

  • List Directory/File (ls): Given a path, return all files and folders in lexicographic order. If it's a file, return just that filename.
  • Create Directory (mkdir): Create a new directory (and any missing parent directories) at the given path.
  • Write/Append to File (addContentToFile): Create a new file with content, or append to existing file content.
  • Read File (readContentFromFile): Return the complete content of a file.

Think of it like implementing your own version of a simplified Unix file system commands!

Input & Output

Basic Operations
$ Input: fs = FileSystem() fs.ls("/") # [] fs.mkdir("/a/b/c") fs.addContentToFile("/a/b/c/d", "hello") fs.ls("/") # ["a"] fs.readContentFromFile("/a/b/c/d") # "hello"
โ€บ Output: [] ["a"] "hello"
๐Ÿ’ก Note: Create directory structure, add file with content, then list and read operations work correctly
File vs Directory Listing
$ Input: fs = FileSystem() fs.addContentToFile("/goowmfn", "c") fs.ls("/goowmfn") # ["goowmfn"] fs.ls("/") # ["goowmfn"]
โ€บ Output: ["goowmfn"] ["goowmfn"]
๐Ÿ’ก Note: When ls() is called on a file path, it returns just the filename. When called on directory containing that file, it lists the file.
Content Appending
$ Input: fs = FileSystem() fs.addContentToFile("/a", "hello") fs.addContentToFile("/a", " world") fs.readContentFromFile("/a") # "hello world"
โ€บ Output: "hello world"
๐Ÿ’ก Note: Multiple calls to addContentToFile append content to existing files rather than overwriting

Visualization

Tap to expand
๐Ÿ—‚๏ธ File System Tree Structure/homeusrvaralicebobresume.pdfphotosls("/home/alice")Path: /home/alice1. Start at root2. Go to 'home' node3. Go to 'alice' node4. List contentsResult: ["photos", "resume.pdf"]๐ŸŽฏ Each operation is O(path_depth) - lightning fast!
Understanding the Visualization
1
Root Foundation
Start with root node (/) as the foundation of our file system tree
2
Branch Navigation
Each path component leads us down a specific branch in the tree
3
Efficient Access
Operations only traverse the path depth, not the entire file system
Key Takeaway
๐ŸŽฏ Key Insight: By representing the file system as a tree where each node is a directory containing files and subdirectories, we achieve O(path_depth) complexity for all operations - much faster than linear searching through all paths.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(L)

Where L is the length of the path (number of directories in path). Each operation only needs to traverse the path once.

n
2n
โœ“ Linear Growth
Space Complexity
O(N * L)

Where N is total number of files/directories and L is average path depth. Shared prefixes save space compared to storing full paths.

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค path.length, filePath.length โ‰ค 100
  • 1 โ‰ค content.length โ‰ค 50
  • All paths are absolute paths starting with '/' and do not end with '/' except for root
  • At most 300 calls will be made to the methods
  • File and directory names consist of only lowercase English letters and digits
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen