Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Design In-Memory File System



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Design a Simple In-Memory File System

Design a Simple In-Memory File System

Problem Statement

Create an in-memory file system that supports the following operations:

  • list: Given a string path, if the path points to a file, return a list containing only the file's name. If it points to a directory, return a list of all files and directories inside it. The returned list should be sorted in alphabetical order.
  • makeDir: Given a directory path that doesn't exist, create all directories along the path. This method does not return anything.
  • appendToFile: Given a file path and some content, if the file doesn't exist, create it with the given content. If it exists, append the content to the file. This method does not return anything.
  • readFile: Given a file path, return the content stored in the file as a string.

Examples

Input:
["SimpleFS","list","makeDir","appendToFile","list","readFile"]
[[],["/"],["/x/y/z"],["/x/y/z/file1","world"],["/x/y"],["/x/y/z/file1"]]

Output:
[null,[],null,null,["z"],"world"]

Explanation:
- The file system is initialized.
- Listing the root directory returns an empty list.
- Creating directory path "/x/y/z".
- Adding content "world" to the file "/x/y/z/file1".
- Listing directory "/x/y" returns ["z"] because "z" is a directory inside "/x/y".
- Reading the content of "/x/y/z/file1" returns "world".
    

Constraints

  • All paths are absolute and start with /, except the root which is just /.
  • All directory and file names contain only lowercase English letters.
  • No duplicate file or directory names exist within the same directory.
  • All method calls will be made with valid paths and parameters.
  • The depth of the directory structure will not exceed 100 levels.