← 返回 perplexity 的题目列表Design an In-Memory File System (Trie-based)
类型:online_judge
Problem: Design an In-Memory File System
Implement an in-memory file system that supports creating directories, writing to files, reading file contents, and listing directory contents in lexicographic order.
APIs to implement
Design a class FileSystem with the following methods:
ls(path)
Input: string path
Output: list of strings representing the entries under path
Rules:
If path is a file path, return a single-element list containing only the file name.
If path is a directory path, return all child directory names and file names under it, sorted lexicographically.
mkdir(path)
Input: string path
Effect: recursively create all directories along the path (like mkdir -p).
addContentToFile(filePath, content)
Input: string filePath, string content
Effect:
Create the file if it does not exist.
Append content to the end of the file.
readContentFromFile(filePath)
Input: string filePath
Output: the full content of the file as a string.
Constraints / Notes
Paths are separated by /.
The root directory is /.
Directory/file names consist of lowercase letters only (you may assume this).
Must handle nested directories and multiple appends.
Sample Tests
Operations:
mkdir("/a/b/c")
addContentToFile("/a/b/c/d", "hello")
ls("/")
Expected output:
["a"]
Operations:
readContentFromFile("/a/b/c/d")
Expected output:
"hello"
Operations:
addContentToFile("/a/b/c/d", " world")
readContentFromFile("/a/b/c/d")
Expected output:
"hello world"
Operations:
ls("/a/b")
Expected output:
["c"]
Operations:
ls("/a/b/c")
Expected output:
["d"]
Example
Input
mkdir /a/b/c
addContentToFile /a/b/c/d hello
ls /
Output
["a"]