Practice/Uber/Leetcode 588. Design In-Memory File System
Leetcode 588. Design In-Memory File System
CodingMust
Problem
Design and implement an in-memory virtual file system that mimics basic file system operations. Your implementation should support creating directories, listing directory contents, creating files with content, appending to existing files, and reading file contents.
The file system starts with a root directory /. All paths are absolute paths beginning with /, and path components are separated by forward slashes. Your system must correctly distinguish between files (which contain data) and directories (which can contain other files or directories).
You need to implement the following operations:
- ls(path): If the path points to a file, return a list containing only that filename. If it points to a directory, return a list of all file and directory names in that directory, sorted in lexicographic order.
- mkdir(path): Create a directory at the given path. If any parent directories don't exist, create them automatically.
- add_content_to_file(file_path, content): If the file doesn't exist, create it with the given content. If it exists, append the content to the existing file content.
- read_content_from_file(file_path): Return the complete content of the file at the given path.
Requirements
- Implement a tree-based structure to represent the hierarchical file system
- Correctly differentiate between files and directories
- Support automatic creation of intermediate directories when needed
- Maintain lexicographic ordering when listing directory contents
- Handle file content storage and retrieval efficiently
- Parse paths correctly to navigate through the directory tree
Constraints
- Path length: 1 ≤ length ≤ 500
- All paths are valid absolute paths starting with
/
- File and directory names contain only lowercase letters, digits, and dots
- Content string length: 0 ≤ length ≤ 10,000
- Total number of operations: ≤ 10,000
- Paths will not have trailing slashes (except root
/)
- You can assume all inputs are valid (e.g., reading from existing files)
Examples
Example 1:
`
Operations:
fs = FileSystem()
fs.mkdir("/home/user")
fs.ls("/")
fs.add_content_to_file("/home/user/file.txt", "Hello")
fs.ls("/home/user")
fs.read_content_from_file("/home/user/file.txt")
Output:
["home"]
["file.txt"]
"Hello"
Explanation:
- Created /home/user directory (including parent /home)
- Listed root directory showing "home"
- Created file with content "Hello"
- Listed /home/user showing "file.txt"
- Read file content returning "Hello"
`
Example 2:
`
Operations:
fs = FileSystem()
fs.add_content_to_file("/app.log", "Error: ")
fs.add_content_to_file("/app.log", "Connection failed")
fs.read_content_from_file("/app.log")
fs.ls("/app.log")
Output:
"Error: Connection failed"
["app.log"]
Explanation:
- First add creates the file with "Error: "
- Second add appends "Connection failed"
- Reading returns concatenated content
- Listing a file path returns just that filename
`
Example 3:
`
Operations:
fs = FileSystem()
fs.mkdir("/var/log")
fs.mkdir("/var/data")
fs.add_content_to_file("/var/config.txt", "settings")
fs.ls("/var")
Output:
["config.txt", "data", "log"]
Explanation: Directory listing includes both files and subdirectories in alphabetical order
`