Learning on Web Dev Open is free for all.

Interview Prep · Low Level DesignDesign an in-memory file system
← Low Level Design

Design an in-memory file system

Medium35 minFree, no account

A tree, a path parser, and the two or three operations everyone gets subtly wrong.


The question

Implement an in-memory file system supporting mkdir, addContentToFile, readContentFromFile and ls.

Then add mv and a search by glob pattern.

fs.mkdir("/a/b/c");                  // creates every missing level
fs.addContentToFile("/a/b/c/d", "hello");
fs.readContentFromFile("/a/b/c/d");  // "hello"
fs.ls("/a/b/c");                     // ["d"]
fs.ls("/a/b/c/d");                   // ["d"]: a file lists itself
35:00Commit to an answer before you open the solution. Reading it first teaches you to recognise good answers, which is not the skill being tested.

Stuck?

0 of 3 hints taken

The worked solution

written by a person · not a grade

Score yourself

0 of 5 marked
  • Composite model with a shared node abstraction20
  • Path parsing handling root, trailing slashes and ..25
  • ls asymmetry between files and directories20
  • addContentToFile appends; mkdir creates intermediate levels20
  • mv guarded against moving a directory into itself15

We run no AI here and nothing on this page grades you. The score is yours, and the useful number is the one you get on the same problem a month from now, cold.

kept in this browser only