codejump
MediumPro challengePythonJavaScriptTypeScript

System Design — In-Memory File System

System DesignTreesHash MapDesign

Model a tiny file system as a tree of directories and files.

solve(args, ops) builds new FileSystem() (an empty root /) and replays ops. Paths are
absolute, start with / and use / as the separator; names contain no slash.

  • ls(path) — if path is a directory, return the names of its entries (files and

directories) in lexicographic order; if it is a file, return an array holding just the
file's name.

  • mkdir(path) — create the directory, including any missing parents. Nothing happens if it

already exists. Returns nothing.

  • addcontent(path, content) — create the file if needed (with any missing parent directories)

and append content to it. Returns nothing.

  • read(path) — return the full content of the file.

Only valid operations are tested (no listing of a missing path, no reading a directory).

Example

ls("/") → []    mkdir("/a/b/c")    addcontent("/a/b/c/d","hello")    ls("/") → ["a"]
read("/a/b/c/d") → "hello"    ls("/a/b/c/d") → ["d"]    ls("/a/b/c") → ["d"]

Sample tests

Test #1Classic example
Input: [[],[["ls","/"],["mkdir","/a/b/c"],["addcontent","/a/b/c/d","hello"],["ls","/"],["read","/a/b/c/d"],["ls","/a/b/c/d"],["ls","/a/b/c"],["addcontent","/a/b/c/d"," world"],["read","/a/b/c/d"]]]
Output: [[],null,null,["a"],"hello",["d"],["d"],null,"hello world"]
Test #2Sorted listing and idempotent mkdir
Input: [[],[["mkdir","/z"],["mkdir","/m"],["mkdir","/a"],["ls","/"],["mkdir","/m"],["ls","/m"]]]
Output: [null,null,null,["a","m","z"],null,[]]
Test #3Files and directories side by side
Input: [[],[["addcontent","/x/b.txt","1"],["addcontent","/x/a.txt","2"],["mkdir","/x/dir"],["ls","/x"],["read","/x/a.txt"]]]
Output: [null,null,null,["a.txt","b.txt","dir"],"2"]