In-Memory File System

Hierarchical Virtual File System

Design a file system that lives in memory: a tree of directories and files with mkdir, ls, write, read, rm and mv, correct path handling and clear errors.

Overview

An in-memory file system is a tree. Directories are the inner nodes, files are the leaves, and every path such as /home/ana/notes.txt is a walk from the root down that tree, one name at a time. The system offers the everyday commands: make a directory, list one, write to a file, read it back, remove things and move them.

The problem tests three things. First, whether you model files and directories so that code which walks the tree does not care which one it holds; that is the Composite pattern. Second, whether you parse paths carefully: extra slashes, ., .. and the root are where most bugs live. Third, whether you guard the operations that can break the tree, such as moving a directory inside itself, and say how you would make it safe for several threads.

A strong answer draws the tree, writes one walk helper that every command uses, lists the error cases before coding, and ends with a demo that exercises each of them.

Scoping the problem

The interviewer leaves a lot open, so ask seven questions first. Each matters for a reason, and each has a usual answer.

  • Absolute paths only, or a current directory too? A current directory adds per-session state. Accept absolute paths, and resolve . and .. inside them.
  • Does write replace or append? The answer changes the File API. It appends, and creates the file if it is missing.
  • Does mkdir create missing parents? That is the question of behaving like mkdir -p or not. It does.
  • What does ls on a file return? It is a common edge case. Return a list with just the file name.
  • Must ls be sorted? That is a choice between a sorted dict and sorting on read. Sort by name.
  • Remove and move, or only the four core commands? rm and mv bring the hard cases. Support both, with errors for bad moves.
  • Many threads at once? This decides the locking. Yes, and operations must not corrupt the tree.

Functional requirements

  • mkdir(path) creates a directory and any missing parents
  • ls(path) returns the sorted names in a directory, or [name] when path is a file
  • write(path, text) creates the file (and missing parents) or appends to it
  • read(path) returns the whole content of a file
  • rm(path, recursive) removes a file or an empty directory; a full directory only when recursive is true
  • mv(src, dst) moves or renames a file or directory; refuses a taken name or a move into itself
  • du(path) returns the bytes stored under a path; find(path, name) returns every matching path
  • Paths are absolute; extra slashes, . and .. are resolved

Non-functional requirements

  • Resolving a path costs O(depth): one dict lookup per level
  • Every failure raises a named error; nothing fails silently or returns an empty value by mistake
  • Safe under many threads: every public method holds the tree lock
  • Path parsing is a pure function, separate from the tree
  • New node types (a symlink, for example) add a Node subclass without touching the walk of existing ones

Interview questions

  • Why use the Composite pattern here, and where does it stop helping?
  • How do you make it safe for many threads, and how would you reduce lock contention?
  • How would you add symbolic links?
  • How would you persist this file system or make it survive a crash?
  • Large files: what changes if a file can be gigabytes?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio