title: Return the Boss in the hierarchy -- Tried to apply Depth First Search
body: <p>XYZ is a company which has CEO Bill and a hierarchy of employees. Employees can have a list of other employees reporting to them, which can themselves have reports, and so on. An employee with at least one report is called a manager.
  Please implement the closestCommonManager method to find the closest manager (i.e. farthest from the CEO) to two employees. You may assume that all employees eventually report up to the CEO.</p>
  
  <p>Sample Data: 
  CEO Bill has 3 employees reporting to him: {Dom, Samir, Michael} </p>
  
  <p>Dom has three reports { Peter, Bob, Porter}</p>
  
  <p>Samir has no reports {} Michael has no reports {} </p>
  
  <p>Peter has 2 reports {Milton, Nina} Bob has no reports {} </p>
  
  <p>Porter has no reports {} Milton has no reports {} </p>
  
  <p>Nina has no reports {}</p>
  
  <p>Sample calls: 
  closestCommonManager(Milton, Nina) = Peter</p>
  
  <p>closestCommonManager(Nina, Porter) = Dom </p>
  
  <p>closestCommonManager(Nina, Samir) = Bill </p>
  
  <p>closestCommonManager(Peter, Nina) = Peter</p>
  
  <p>Now, to solve this problem i have approached like this - but i haven't got the solution. 
  I have tried to use simple DFS algorithm, but couldn't complete the solution.</p>
  
  <pre><code>    public static Employee closestCommonManager(Employee ceo, Employee firstEmployee, Employee secondEmployee)
      {
          var visited = new HashSet&lt;Employee&gt;();
          bool firstFound = false, secondFound = false;
  
          Stack&lt;Employee&gt; stack = new Stack&lt;Employee&gt;(); // DFS
          stack.Push(ceo);
  
          while (stack.Count != 0)
          {
              Employee current = stack.Pop();
              IList&lt;Employee&gt; employeeList = current.getReports();
  
              if (firstEmployee.getId() == current.getId())
              {
                  firstFound = true;
              }
              else if (secondEmployee.getId() == current.getId())
              {
                  secondFound = true;
              }
  
              if (firstFound &amp;&amp; secondFound)
                  return current;
              // Should i return previous one? how do i keep track of the
              // node which i found first in hierarchy ???
  
  
              Console.WriteLine(current.getName());
  
              foreach (var employee in employeeList)
              {
                  if (visited.Add(employee))
                  {
                      stack.Push(employee);
                  }
              }
          }
  
          return null;
      }
  </code></pre>