Design In-Memory File System
Understand and solve the interview question "Design In-Memory File System".
We'll cover the following
You have to design an in-memory file system. The skeleton for the class
FileSystem is provided to you. You have to simulate the following functions:
ls: You are given a string representing a path. If it is a file path, return a list that only contains the file’s name. If it is a directory path, return the list of file and directory names in this directory. Your function should return the output (file and directory names together) in lexicographical order.
mkdir: Given a directory path that does not exist, you need to make a new directory according to the path. The function should create all the middle directories in the path if they do not already exist. This function return type is
add_content_to_file: You are given a file path and file content in string format. If the file doesn’t exist, you need to create that file containing the given content. If the file already exists, you need to append the given content to the original content. This function return type is
read_content_from_file: Given a file path, return its content in string format.
Let’s look at an example input and output chart:
||null||The constructor call returns nothing.|
||||Initially, the directory
||null||Create a directory
||null||Create a file
||“File”||Output the file content.|
You can assume all file or directory paths are absolute paths that begin with
/, and do not end with
/except the path that is just
You can assume that all operations will be passed using valid parameters. Users will not attempt to retrieve file content or list a non-existent directory or file or add a non-existent directory file.
You can assume that all directory and file names only contain lower-case letters, and the file and directory names are unique.