I recently interviewed at a small Rochester based company. I was asked to do a take home challenge. The challenge had a question where I was asked to find all the employee that report to person A (so basically direct reports of A and sum of all the direct reports of A's children).
A
/ \
B C
/ \
D E
input: A
output: 4Cycle:
A
/ \
B C
/ \
D E
/
A
Input: A
Output: 4 or graceful coverage of this cycleI solved the question using Depth First Search. Few days later I got an email, saying that the team would like to interview me for next round which will be a code walkthru. In the code walkthru as I was explaining my code, and saying how it this iterative approach is better than recursive, one of the interviewer asked about how will you handle if there is cycle.
I had not handled this condition and made a small adjustment in the DFS function to compare all the direct reports with the initial employee passed. The 2 should never be equal, if they are equal than that means cycle exists. And this is where I guess it went south. The interviewer kept pressing me if this is corret, are you sure this will work. I tried explaining the DFS algo but after 3 attempts he gave up without fully convinced about the accuracy of the approach. He was expecting a recursive solution, so I got a sense maybe he is not able to understand or maybe I am being incorrect here. We had a good 10 min of back and forth
After 1 week I heard back from the recuiter and he mentioned that the feedback is ok and not great. Part of the feedback is that I was not able to pick up hint and change approach when I was given hints. He also said with some mentorship I can get to next level. I am expecting to get a lowball offer, which would be probably becuase of this feedback.
I have now tested my below code and it takes care of the cyclic order like I mentioned in the interview.
My question how should I approach this? Can I let my recuruiter know about this? Or email them showcasing with a testcase how the code works?
Its a senior software engineer at a small company.
Initial Code:
int GetEmployeeReports(Employee employee)
{
Stack<Employee> empStack = new Stack<Employee>();
empStack.Push(employee);
var count = 0;
while (empStack.Count != 0)
{
Employee currentEmp = empStack.Pop();
if (currentEmp != null && (currentEmp.DirectReports != null))
{
foreach (Employee directRep in currentEmp.DirectReports)
{
count++;
empStack.Push(directRep);
}
}
}
return count;
}
Code with fix proposed in interview:
int GetEmployeeReports(Employee employee)
{
Stack<Employee> empStack = new Stack<Employee>();
empStack.Push(employee);
var count = 0;
while (empStack.Count != 0)
{
Employee currentEmp = empStack.Pop();
if (currentEmp != null && (currentEmp.DirectReports != null))
{
foreach (Employee directRep in currentEmp.directReps)
{
if(directRep.EmployeeId != employee.EmployeeId)
{
count++;
empStack.Push(directRep);
}
}
}
}
return count;
}