Hi,
I got this question in dropbox in tech screening. And I am also posting my solution which I gave but I am very disappinted as I got reject. Please give me suggestion as to what could have been done better. There was alot of dicussion later as well with small modfications in this code., but I am skipping that discussion for now.
Given the list of Directories and a hashmap to which directories the user has access to. If a parent has permission, all the children will have permissions as well.
Given a directory name, return true or false if the user will have permissions or not.
Example:
Input:
A
----->B
---------->H
---------->I
----->C
---------->D
---------->E
--------------->F
--------------->G
Access to the directory: E & I
Also given the directory name for which we are checking permissions
Ouput:
// given G - return true as the parent E has access
// Given D - return false as neither D nor parent of D has access
// Given H - return false
// Given I - return true
public class FileSystem
{
public void Declare()
{
// This is child and parent pair provided
string[] s9 = { "A", null };
string[] s1 = { "B", "A"};
string[] s2 = { "C", "A" };
string[] s3 = { "D", "C" };
string[] s4 = { "E", "C" };
string[] s5 = { "F", "E" };
string[] s6 = { "G", "F" };
string[] s7 = { "H", "B" };
string[] s8 = { "I", "J" };
List<string[]> childParentList = new List<string[]>();
childParentList.Add(s1);
childParentList.Add(s2);
childParentList.Add(s3);
childParentList.Add(s4);
childParentList.Add(s5);
childParentList.Add(s6);
childParentList.Add(s7);
childParentList.Add(s8);
childParentList.Add(s9);
//Directory where a user has access to
HashSet<string> map = new HashSet<string>();
map.Add("E");
map.Add("I");
// given G - return true as the parent E has access
// Given D - return false as neither D nor parent of D has access
// Given H - return false
// Given I - return true
bool result1 = Implentation(childParentList, map, "G");
bool result2 = Implentation(childParentList, map, "D");
bool result3 = Implentation(childParentList, map, "H");
bool result4 = Implentation(childParentList, map, "I");
}
public bool Implentation(List<string[]> childParentList, HashSet<string> map, string directory)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
for(int i=0; i< childParentList.Count; i++)
{
dict[childParentList[i][0]] = childParentList[i][1];
}
string key = directory;
while(true)
{
if(map.Contains(key))
{
return true;
}
else if(key == null)
{
return false;
}
else
{
key = dict[key];
}
}
}
}