Design In-Memory File System

Understand and solve the interview question "Design In-Memory File System".

Description

You have to design an in-memory file system. The skeleton for the type 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 void.

AddContentToFile: 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 void.

ReadContentFromFile: Given a file path, return its content in string format.

Example

Let’s look at an example input and output chart:

Operation Output Explanation
newFileSystem FileSystem The function call initalizes the FileSystem structure.
fs.ls("/") [] Initially, the directory / has nothing. So, we return an empty list.
fs.mkdir("/dir1/dir2/dir3") null Create a directory dir1 in /. Then, create a directory dir2 in directory dir1. Lastly, create a directory dir3 in directory dir2.
fs.AddContentToFile("/dir1/dir2/dir3/file1", "File") null Create a file file1 with contents File in the directory /dir1/dir2/dir3.
fs.ls("/") [“dir1”] Only directory dir1 exists in the / directory.
fs.ReadContentFromFile("/dir1/dir2/dir3/file1") “File” Output the file content.

Assumptions

  1. 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 "/".

  2. 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.

  3. You can assume that all directory and file names only contain lower-case letters, and the file and directory names are unique.

Coding exercise

package main
import (
"fmt"
"encoding/json"
)
type FileSystem struct{
// declare your data structures here
}
func newFileSystem() FileSystem {
// Initialize your data structures here
return FileSystem{}
}
func (fs FileSystem) ls(path string) []string{
// write your code here
return []string{}
}
func (fs FileSystem) mkdir(path string) {
// write your code here
}
func (fs FileSystem) AddContentToFile(filePath, content string) {
// write your code here
}
func (fs FileSystem) ReadContentFromFile(filePath string) string{
// write your code here
return "";
}
Design In-Memory File System

Solution

We need a mechanism to represent files and ...