SDE Path

In-Memory File System

Hard

In-Memory File System

mkdir, append, read, ls over a path tree — the Composite pattern rendered as a judge-able problem.

Requirements

  • Paths are absolute, /-separated, no trailing slash (except the root / itself), and components contain no spaces.
  • mkdir and append auto-create missing intermediate directories (like mkdir -p).
  • ls output is sorted — the tree's internal order must not leak into results.
  • The judge never issues contradictory operations (e.g., mkdir over an existing file path).

API to implement

Implement the class FileSystem. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.

| Command | Returns | Behavior | |---|---|---| | mkdir <path> | — | Create directory path (absolute, like /a/b/c), creating all missing intermediate directories. | | append <path> <content> | — | Append content (no spaces) to the file at path, creating the file — and any missing intermediate directories — if needed. | | read <path> | string | Return the file's content, or - if the path doesn't exist, isn't a file, or is empty. | | ls <path> | string | For a directory: its children's names, lexicographically sorted, space-joined (- if empty or missing). For a file: just the file's own name. |

Input format

The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.

Output format

For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.

This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.

Sample cases

Example 1
Input
10
mkdir /a/b/c
ls /
ls /a
ls /a/b/c
append /a/b/f.txt hello
ls /a/b
read /a/b/f.txt
append /a/b/f.txt world
read /a/b/f.txt
ls /a/b/f.txt
Expected output
a
b
-
c f.txt
hello
helloworld
f.txt
Example 2
Input
5
append /root.txt x
ls /
read /root.txt
read /missing
ls /missing
Expected output
root.txt
x
-
-