Design In-Memory File System - Problem

Design a data structure that simulates an in-memory file system.

Implement the FileSystem class:

  • FileSystem() - Initializes the object of the system
  • List<String> ls(String path) - If path is a file path, returns a list that only contains this file's name. If path is a directory path, returns the list of file and directory names in this directory. The answer should be in lexicographic order
  • void mkdir(String path) - Makes a new directory according to the given path. The given directory path does not exist. If the middle directories in the path do not exist, you should create them as well
  • void addContentToFile(String filePath, String content) - If filePath does not exist, creates that file containing given content. If filePath already exists, appends the given content to original content
  • String readContentFromFile(String filePath) - Returns the content in the file at filePath

Input & Output

Example 1 — Basic File System Operations
$ Input: operations = ["FileSystem", "ls", "mkdir", "addContentToFile", "ls", "readContentFromFile"] parameters = [[], ["/"], ["/a/b/c"], ["/a/b/c/d", "hello"], ["/a/b/c"], ["/a/b/c/d"]]
Output: [null, [], null, null, ["d"], "hello"]
💡 Note: Initialize file system, list root (empty), create directory /a/b/c, add file /a/b/c/d with content "hello", list /a/b/c contents (shows file "d"), read file content (returns "hello")
Example 2 — File Content Append
$ Input: operations = ["FileSystem", "addContentToFile", "readContentFromFile", "addContentToFile", "readContentFromFile"] parameters = [[], ["/a", "hello"], ["/a"], ["/a", " world"], ["/a"]]
Output: [null, null, "hello", null, "hello world"]
💡 Note: Create file /a with "hello", read it (returns "hello"), append " world" to /a, read again (returns "hello world")
Example 3 — Directory Listing
$ Input: operations = ["FileSystem", "mkdir", "mkdir", "addContentToFile", "ls"] parameters = [[], ["/a"], ["/b"], ["/c.txt", "content"], ["/"]]
Output: [null, null, null, null, ["a", "b", "c.txt"]]
💡 Note: Create directories /a and /b, create file /c.txt, then list root directory contents in lexicographic order

Constraints

  • 1 ≤ operations.length ≤ 300
  • 1 ≤ path.length, filePath.length ≤ 100
  • 1 ≤ content.length ≤ 50
  • All paths are absolute paths starting with '/'
  • All inputs consist of lowercase English letters, digits, '/', and '.'
  • You can assume that all operations are valid

Visualization

Tap to expand
In-Memory File System: Operations FlowOperations:mkdir("/a/b")addFile("/a/b/c", "hi")ls("/a/b")rootabcResults:null (mkdir)null (addFile)["c"] (ls)File System Tree Structure with O(path_length) OperationsEach node stores children in hash map for fast lookup
Understanding the Visualization
1
Input Operations
Sequence of file system commands like mkdir, ls, addContentToFile
2
Trie Structure
Build tree where nodes are directories/files with parent-child relationships
3
Output Results
Return appropriate responses: null for void operations, lists for ls, strings for read
Key Takeaway
🎯 Key Insight: Use trie structure where each node has a hash map of children for O(1) lookup and efficient path navigation
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 28
28.0K Views
High Frequency
~35 min Avg. Time
856 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