In-Memory File System
NetflixOnsiteMedium
Build a file system that keeps directories and files in memory. Paths are absolute strings starting with / and split on /; / itself is the root directory. Each name between slashes contains only lowercase letters, digits and .. A path never passes through a file, and a name is never used as both a file and a directory.
Each test creates one FileSystem instance and runs a sequence of [method, [args]] calls. The output lists every call's return value, with None rendered as null.
Part 1: Directories and files
Implement a FileSystem with four methods:
mkdir(path)creates the directory atpath, including any missing parent directories. If the directory already exists, nothing changes.add_content(path, text)appendstextto the file atpath, creating the file and any missing parent directories if needed.read_content(path)returns the full text stored at the filepath. The file always exists.ls(path)returns the names inside the directory atpath, sorted in ascending order. Ifpathis a file, it returns a list containing only that file's name.pathalways exists.
mkdir and add_content return None.
- Example 1
- init
[]- operations
[["mkdir",["/a/b"]],["add_content",["/a/b/c.txt","hello"]],["add_content",["/a/b/c.txt"," world"]],["read_content",["/a/b/c.txt"]],["ls",["/a"]],["ls",["/a/b"]],["ls",["/a/b/c.txt"]],["ls",["/"]]]- Output
[null,null,null,"hello world",["b"],["c.txt"],["c.txt"],["a"]]
Why:
mkdir("/a/b")also creates the missing parent/a. The file receiveshelloand thenworld, so reading it returnshello world. Listing a file returns just that file's name.
Constraints
- 1 <= len(
operations) <= 10^4 - 1 <= len(
path) <= 200 - 1 <= len(
text) <= 100 textcontains only lowercase letters, digits and spaces