Design an in-memory file system

Exclusive Hard 5 Data StructuresDesign PatternsMemory Management Meta

Problem contributed by @yuta_is_a_bum

Implement FileSystem, an in-memory hierarchical file system that supports basic UNIX-like file and directory operations.


Description

Design an in-memory file system that stores everything in RAM and supports hierarchical directories and files identified by absolute paths (like /a/b/c).

This problem is divided into multiple parts. Each part builds on the previous one.


Constructor

FileSystem()

  • Initializes an empty file system.

Part 1: Basic File System Operations

Methods

ls(path)

  • Returns the contents of the directory at path.
  • If path is a directory, return a list of names of files and directories directly under it.
  • If path is a file, return a list containing only the file name.
  • The returned list must be sorted lexicographically.

mkdir(path)

  • Creates a directory at path.
  • If intermediate directories do not exist, create them automatically.
  • If the directory already exists, do nothing.

addContentToFile(filePath, content)

  • Creates or appends content to a file at filePath.
  • If the file does not exist, create it and write content.
  • If the file already exists, append content to its existing contents.
  • Any missing intermediate directories in filePath must be created automatically.
  • Input format: Content is enclosed in double quotes. Use \n for newlines, \t for tabs, \\ for backslash, and \" for quotes within the content.

readContentFromFile(filePath)

  • Returns the full content of the file at filePath.

Example Usage

FileSystem()
ls("/")                      // returns []
mkdir("/a/b/c")
addContentToFile("/a/b/c/d", "hello")
ls("/")                      // returns ["a"]
readContentFromFile("/a/b/c/d")        // returns "hello"
addContentToFile("/a/b/c/d", " world")
readContentFromFile("/a/b/c/d")        // returns "hello world"


Part 2: File and Directory Manipulation

Extend your implementation with the following operations:

rm(path)

  • Removes the file or directory at path.
  • If path refers to a file, delete the file.
  • If path refers to a directory, delete the directory and all of its contents recursively.

mv(source, destination)

  • Moves or renames a file or directory.
  • source is an existing file or directory path.
  • destination is the new path.
  • If destination already exists as a directory, move source inside it.
    • Example: mv("/a/x", "/b") moves /a/x to /b/x if /b is a directory.
  • Otherwise, treat destination as a rename to that exact path.
    • Example: mv("/a/x", "/a/y") renames /a/x to /a/y.

Part 3: File Metadata

Extend the file system to track metadata for all files and directories.

Methods

getSize(path)

  • If path is a file, return the size of the file (number of characters in its content).
  • If path is a directory, return the total size of all files contained within it (recursively).

getLastModified(path)

  • Returns the timestamp of the most recent modification to the file or directory.
  • Modifications include:
    • creation
    • deletion (of any descendant should update ancestors as well, since directory contents changed)
    • content updates
    • moves / renames

Timestamp Parameter

For operations that modify the file system (in Part 3), an optional timestamp parameter may be provided:

  • addContentToFile(filePath, content, timestamp)
  • mkdir(path, timestamp)
  • rm(path, timestamp)
  • mv(source, destination, timestamp)

When a timestamp is provided, use it to update the lastModified metadata. If not provided, you may use any monotonically increasing value.

Creation Time Tracking

Track and store the creation time for all files and directories.


Notes

  • All paths are absolute and start with /.
  • File and directory names consist only of lowercase English letters.
  • You may assume all inputs are valid.
  • Timestamps are monotonically increasing.
  • There are no constraints on how files or directories must be stored internally.
  • Correctness, clarity, and proper handling of hierarchical relationships matter more than micro-optimizations.
  • For testing purposes:
    • Methods that don't return values should output null in the test results.
    • The ls method should output lists in the format [name1, name2, name3] with unquoted names separated by commas and spaces, or [] for empty directories.

Constraints

  • All paths are absolute and start with /.
  • File and directory names consist only of lowercase English letters
  • You may assume all inputs are valid.
  • The file system is entirely in memory

Examples

Example 1
Input:FileSystem ls / mkdir /a/b/c addContentToFile /a/b/c/d "hello" ls / readContentFromFile /a/b/c/d
Output:null [] null null [a] hello
Accepted 1/2
Acceptance 50%
Loading editor...
Sample Input:
FileSystem ls / mkdir /a/b/c addContentToFile /a/b/c/d "hello" ls / readContentFromFile /a/b/c/d
Expected Output:
null [] null null [a] hello