Design an in-memory file system
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
pathis a directory, return a list of names of files and directories directly under it. - If
pathis 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
contentto its existing contents. - Any missing intermediate directories in
filePathmust be created automatically. - Input format: Content is enclosed in double quotes. Use
\nfor newlines,\tfor 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
pathrefers to a file, delete the file. - If
pathrefers to a directory, delete the directory and all of its contents recursively.
mv(source, destination)
- Moves or renames a file or directory.
sourceis an existing file or directory path.destinationis the new path.- If
destinationalready exists as a directory, movesourceinside it.- Example:
mv("/a/x", "/b")moves/a/xto/b/xif/bis a directory.
- Example:
- Otherwise, treat
destinationas a rename to that exact path.- Example:
mv("/a/x", "/a/y")renames/a/xto/a/y.
- Example:
Part 3: File Metadata
Extend the file system to track metadata for all files and directories.
Methods
getSize(path)
- If
pathis a file, return the size of the file (number of characters in its content). - If
pathis 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
nullin the test results. - The
lsmethod should output lists in the format[name1, name2, name3]with unquoted names separated by commas and spaces, or[]for empty directories.
- Methods that don't return values should output
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
FileSystem
ls /
mkdir /a/b/c
addContentToFile /a/b/c/d "hello"
ls /
readContentFromFile /a/b/c/dnull
[]
null
null
[a]
helloFileSystem
ls /
mkdir /a/b/c
addContentToFile /a/b/c/d "hello"
ls /
readContentFromFile /a/b/c/dnull
[]
null
null
[a]
hello