MediumStateful DesignPython 3

In-memory Filesystem

Model directory and file state with predictable path normalization and listing behavior.

35m1 sample tests1 hidden tests

In-memory Filesystem

Implement a tiny filesystem with directories and files.

Requirements

  • Define FileSystem.
  • mkdir(path) creates directories recursively.
  • write_file(path, content) writes or overwrites a file. Parent directory must exist.
  • read_file(path) returns file content.
  • ls(path) returns sorted child names for a directory, or [filename] for a file.
  • Missing paths raise FileNotFoundError.

Example

python
1fs = FileSystem() 2fs.mkdir("/docs") 3fs.write_file("/docs/readme.md", "hello") 4assert fs.ls("/") == ["docs"] 5assert fs.ls("/docs/readme.md") == ["readme.md"] 6assert fs.read_file("/docs/readme.md") == "hello"

Constraints

  • Use standard-library Python only.
  • Normalize repeated slashes and trailing slashes.

Editor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Sample Tests
creates directories and reads files
from solution import FileSystem

fs = FileSystem()
fs.mkdir("/docs/guides")
fs.write_file("/docs/guides/readme.md", "hello")
assert fs.ls("/") == ["docs"]
assert fs.ls("/docs") == ["guides"]
assert fs.ls("/docs/guides/readme.md") == ["readme.md"]
assert fs.read_file("/docs/guides/readme.md") == "hello"
Results
Run sample tests or submit all tests.