Design an In-Memory File System
📁 What is an in-memory file system? It is a small version of the tree your operating system keeps on disk: folders that hold files and other folders, all reached through paths like
/docs/plan.txt. "In memory" means nothing is saved to disk. The whole tree lives in objects while the program runs, which keeps the problem about modeling, not about storage.
This problem shows up in interviews in two forms. Some interviewers ask for exactly the operations a shell gives you: mkdir, ls, write a file, read it back. Others add "how big is this folder?" and "delete this folder and everything in it". Both are the same design. The tree, the path walk and the split between files and directories do not change; the extra operations are a few lines each once the tree is right.
Requirements
"Design an in-memory file system. It should support creating directories, listing them, and writing and reading files."
That one sentence hides a lot of choices. What does a path look like? What happens when a folder in the middle of a path is missing? The questions below pin down each rule before we write code.
Clarifying Questions
You: What do paths look like? Do we need relative paths, cd, . and ..?
Interviewer: Only absolute paths, like /a/b/c.txt. The root is /. No trailing slashes, no . or .., and no current directory.
You: If I create /a/b/c and /a does not exist yet, what happens?
Interviewer: Create everything that is missing, like mkdir -p. But if something on the way is a file, that is an error.
You: And when writing /x/y.txt while /x does not exist?
Interviewer: That fails. Writing a file never creates folders. Writing to an existing file replaces its content. We also want an append operation that adds to the end.
You: What should listing return, and in what order?
Interviewer: For a directory, the names of its children, sorted. For a file, just that file's name. For a path that does not exist, an empty list.
You: What is the size of a directory?
Interviewer: The total size of every file inside it, at any depth, like du. A file's size is the length of its content.
You: Can I delete a directory that still has files in it?
Interviewer: Yes, it goes away with everything inside, like rm -r. You cannot delete the root.
You: Do several threads use it, and do we save anything to disk?
Interviewer: One thread, nothing saved. We may talk about both at the end.
The third answer is the one candidates most often skip. mkdir creates missing parents but writeFile does not, so the two methods need slightly different walks down the tree. If you assume both behave the same, your code will pass your own examples and fail the interviewer's.
💡 Interview tip: Ask what every method returns when something is missing or in the way. File systems have many "almost valid" inputs: a file where a folder should be, a folder where a file should be, the root. Writing those cases down as rules early stops you from discovering them halfway through the code.
Final Requirements
- The file system is a tree with a root directory
/. Directories hold files and directories; files hold text. - Paths are absolute: names joined by
/, starting with/. A name is unique inside its directory. mkdir(path)creates the directory and any missing directories above it. It fails if any part of the path is a file.ls(path)returns the sorted child names of a directory, or the name of a file, or an empty list if nothing is there.writeFile(path, content)creates a file or replaces its content;appendFile(path, content)creates it or adds to the end. Both fail if the parent directory does not exist or ifpathis a directory.readFile(path)returns a file's content, or""for a missing path or a directory.size(path)returns a file's length or the total length of all files under a directory;-1if nothing is there.remove(path)deletes a file or a whole directory subtree. It fails for a missing path and for the root.
Out of scope
- Relative paths, a current directory,
.and..(see Extensibility). - Permissions, owners, timestamps and symbolic links.
- Saving to disk, and several threads at once (both come back in Extensibility).
- Searching the tree with filters. That is its own problem: /low-level-design/unix-file-search.