Design In-Memory File System
Explore how to design an in-memory file system by implementing functions such as ls to list directory contents, mkdir to create directories, and file content handling. This lesson helps you master directory and file management concepts often tested in coding interviews and improve your problem-solving skills.
We'll cover the following...
Statement
Design an in-memory file system. The skeleton for the class FileSystem is provided to you. Simulate the following functions:
-
ls(String path): If path is a file path, return a list that only contains the file’s name. If it’s a directory path, return the list of files and directory names in this directory. Your function should return the output (file and directory names together) in lexicographical order.
-
Void mkdir(String path): If the given path does not exist, make a new directory according to it. The function should create all the middle directories in the path if they don’t exist.
-
Void addContentToFile(String filePath, String content): If the file doesn’t exist, create that file containing the given content. If the file already exists, append the given content to the original content.
-
Void readContentFromFile(String filePath): Return given file’s content in string format.
Note: You may assume that all directory names and file names only contain lowercase letters, and the same names will not exist in the same directory. ...