Salesforce Interview Question
Software DevelopersCountry: United States
Interview Type: Written Test
File system is basically a tree. So your root node is your root folder. Each folder needs to be have map with name of the subdirectories.
Since we change directories we need to keep reference to current directory and have ability to traverse to its parent for 'up' command.
So directory can be represented as a class with reference to its parent object (parent will be null for the root) and its map of sub directories.
Command that needs to be handled:
dir -> print element of current node's map
mkdir -> create element in map. Entry in the map should be object of class node and should have reference to current node as a parent.
if map already contains that entry with the key -> subdirectory already exists
cd -> change current node value
Find entry in map of current with name of directory you want to cd into.
if map doesn't contain entry with that name -> subdirectory doesn't exist.
if exists then change current node
up -> change current node value
Check if parent reference is null
if null -> cannot move up the root directory
else change current node to its parent.
I think it is easy to use Trie in this case. Define trie with maps, where each value in map is an entity (file or directory). Maintain a stack (say currPath) at class level which serves as current path of the cursor (pwd in unix). so if you want to create a file or directory, go in the depth of trie and when path is equal to currPath, do the intended operation.
Can anyone help me
- asrikanth.125 November 18, 2016