Design an Advanced File System
Solve a hard-level problem for developing a file system using tries.
Problem statement
Design a file system that allows the user to perform the following operations.
ls
: Allows the users to list all the files and folders present in a directory path. The function should take in the directory path as input and return a list of all files and folder names present in this directory.
mkdir
: Allows the users to create new directories. The function takes the directory name as the input and creates a new directory in the current directory.
pwd
: Returns the path to the present working directory.
cd
: Allows the users to enter a directory. The user cannot enter a file using this command.
touch
: Allows the user to create a new file in the current directory. If a file with the same name already exists in the directory, it shows an error.
Example
Sample input
{"mkdir", "home"},{"ls"},{"cd", "/home"},{"pwd"},{"touch", "newFile.txt"},{"touch", "newFile.txt"},{"touch", "newFile.csv"},{"ls"},{"mkdir", "Ben"},{"cd", "/Ben"},{"mkdir", "Movies"},{"mkdir", "College"},{"ls"},{"pwd"},{"cd" , "/Movies"},{"touch", "HarryPotter.mp4"},{"pwd"},{"ls"},{"cd", "/~"},{"cd", "/home/Ben/Movies"},{"ls"},{"pwd"},{"cd", "/Narnia"}
Sample output
Created directory homehomeChanged to directory/homeCreated newFile.txtnewFile.txt already existsCreated newFile.csvnewFile.csv newFile.txtCreated directory BenChanged to directoryCreated directory MoviesCreated directory CollegeCollege Movies/home/BenChanged to directoryCreated HarryPotter.mp4/home/Ben/MoviesHarryPotter.mp4Changed back to root directoryChanged to directoryHarryPotter.mp4/home/Ben/MoviesFolder name Narnia not found. Cannot change directory.
Try it yourself
Try to solve the problem yourself before reading the solution.
#include<iostream>#include<vector>#include<unordered_map>#include<set>using namespace std;class FileSystem {public:FileSystem(){}vector<string> ls() {// your code goes herereturn {};}string mkdir(string dirName) {// your code goes herereturn "";}string cd(string path){// your code goes herereturn "";}string pwd(){// your code goes herereturn "";}string touch(string fileName){// your code goes herereturn "";}};
Intuition
This problem is an extension of and similar to the previous problem. We solve it in a similar fashion using tries.
Custom trie node and pointer to working directory
//Definition of a trie nodeclass TrieNode{public:// map which stores the pointers from// folder names to the children nodesunordered_map<string, TrieNode*> children;// a flag to identify if a// node represents a filebool isFile;// to store the aboslute path of the file or directorystring absolutePath;};
Our custom trie node for this problem will contain three parameters:
absolutePath
: The string representing the complete or absolute path of the current file or directory. The absolute path means a complete location of the file relative to the root directory.
isFile
: A flag determining if the current node represents a file or a dictionary.
children
: A dictionary representing the outgoing connections to child files or directories.
Algorithm
We can implement the required functions using the following algorithm.
ls
: Listing the files and directories
For listing the contents of a directory:
Traverse down the trie up to the last folder of the directory path.
Iterate through all the children of the current trie node. If the path is a file, return the file name, or list all the folders present under the current directory.
Let's try to understand this better with an example.
mkdir
: Making a new directory
This command creates a new directory in the user's current location (directory). If the user's current working directory is /home/Ben/
and the user types the command mkdir Movies
, a new directory will be created inside the path /home/Ben
. The absolute path of the new directory will be /home/Ben/Movies
.
We'll maintain a pointer to the current working directory. Therefore, to create a new directory in the current path, add a new child in the children's map of the current trie node. The key would be the name of the new directory being added, and the value is the pointer to the newly created trie node. An error is returned if a folder with the same name already exists in the location.
Let's try to understand ...