Design In-Memory File System

Try to solve the Design In-Memory File System problem.

Statement

Design an in-memory file system. The skeleton for the class FileSystem is provided to you. Simulate the following functions:

  • ls: You’re 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 files and directory names in this directory. Your function should return the output (file and directory names together) in lexicographical order.

  • mkdir: If you have a directory path that does not exist, make a new directory according to the given path. The function should create all the middle directories in the path if they don’t exist.

  • AddContentToFile: You’re given a file path and file content in string format. 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.

  • ReadContentFromFile: Given a file path, return its 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. Additionally, you may assume that all operations will be passed valid parameters, and an attempt to retrieve file content or list a directory or file that does not exist is not allowed.

Constraints:

  • 1 ≤\leq path.length, file_path.length ≤100\leq 100

  • 1≤1 \leq content.length ≤50\leq 50

  • path and file_path are absolute paths that start with  '/' and do not end with '/' unless the path is simply '/'.

  • At most 300300 calls can be made to ls, mkdir, AddContentToFile, and ReadContentFromFile.

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.