Microsoft Interview Question
Software Engineer / Developers Software Development ManagersCountry: United States
The solution is good, but not sufficient. The hierarchy level is not fixed and could go much deeper like - CEO-> Vps-> GMs ->PMs -> LDs -> SE->...
Well, in that case, keeping only one class to manage everything is inevitable. If you know the whole hierarchy you can make all classes and do what I did. For now I propose this method to handle the scenario that you do not know the whole hierarchy.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MS_Question
{
class Program
{
Human ceo;
Program()
{
Human w1 = new Human(Human.humanType.EM, Human.humanType.Nothing); w1.name = "w1";
Human w2 = new Human(Human.humanType.EM, Human.humanType.Nothing); w1.name = "w2";
Human w3 = new Human(Human.humanType.EM, Human.humanType.Nothing); w1.name = "w3";
Human w4 = new Human(Human.humanType.EM, Human.humanType.Nothing); w1.name = "w4";
Human w5 = new Human(Human.humanType.EM, Human.humanType.Nothing); w1.name = "w5";
Human w6 = new Human(Human.humanType.EM, Human.humanType.Nothing); w1.name = "w6";
Human w7 = new Human(Human.humanType.EM, Human.humanType.Nothing); w1.name = "w7";
Human w8 = new Human(Human.humanType.EM, Human.humanType.Nothing); w1.name = "w8";
Human gm1 = new Human(Human.humanType.GM, Human.humanType.EM); gm1.name = "gm1";
gm1.directUnderneath.Add(w1); gm1.directUnderneath.Add(w2);
Human gm2 = new Human(Human.humanType.GM, Human.humanType.EM); gm2.name = "gm2";
gm2.directUnderneath.Add(w3); gm2.directUnderneath.Add(w4);
Human gm3 = new Human(Human.humanType.GM, Human.humanType.EM); gm3.name = "gm3";
gm3.directUnderneath.Add(w5); gm3.directUnderneath.Add(w6);
Human gm4 = new Human(Human.humanType.GM, Human.humanType.EM); gm4.name = "gm4";
gm4.directUnderneath.Add(w7); gm4.directUnderneath.Add(w8);
Human vp1 = new Human(Human.humanType.VP, Human.humanType.GM); vp1.name = "vp1";
vp1.directUnderneath.Add(gm1); vp1.directUnderneath.Add(gm2);
Human vp2 = new Human(Human.humanType.VP, Human.humanType.GM); vp2.name = "vp2";
vp2.directUnderneath.Add(gm3); vp2.directUnderneath.Add(gm4);
Human ceo1 = new Human(Human.humanType.CEO, Human.humanType.VP); ceo1.name = "ceo1";
ceo1.directUnderneath.Add(vp1); ceo1.directUnderneath.Add(vp2);
this.ceo = ceo1;
}
public Human findWorker(Human h, String name){
if(h.name.Equals(name))
return h;
else
{
IEnumerable<Human> res = h.directUnderneath.Select(i => i).Where( i => i.name.Equals(name));
foreach (Human human in res)
{
return human;
}
if (h.directUnderneath.Count != 0)
{
foreach (Human human in h.directUnderneath)
{
return findWorker(human, name);
}
}
}
return null;
}
static void Main(string[] args)
{
Program p = new Program();
Human h = p.findWorker(p.ceo, "w8");
if (h != null)
{
Console.WriteLine(h.name + " Exists.");
}
else
{
Console.WriteLine("No such person exists.");
}
Console.ReadKey();
}
}
class Human{
public enum humanType {CEO,VP,GM,EM,Nothing};
public humanType myType;
public humanType myArrayType;
public string name="";
public List<Human> directUnderneath = new List<Human>();
public Human()
{
}
public Human(humanType mine, humanType myarr)
{
this.myType = mine;
this.myArrayType = myarr;
}
}
}
don't understand the question very well. Not sure what kind of method do you need. How you write the code, it really depends on how the objects look like.
how you write the code, it depends on the design of those classes.
if your design is good, you can just do this
var personDetail = personCollection.SingleOrDefault(n => n.alias == alias && n.title == Title);
otherwise
public personDetail getEmp(string alias, List<person> personCollection = null)
{
// get collection by title
// if = null, we will start at the CEO level Assume, you can only have one CEO
if (personCollection == null)
{
if (CEO.alias == alias)
{
return CEO;
}
else
{
return getEmp(alias, CEO.personCollection);
}
}
else
{
var personDetail = personCollection.SingleOrDefault(n => n.alias == alias);
//if found return the detail
if (personDetail != null)
{
return personDetail;
}
//else try to find match the alias
else
{
foreach (var person in personCollection)
{
if (person.personCollection != null && person.personCollection.Count() > 1)
var person = getEmp(alias, person.personCollection);
if (person != null)
{
return person;
}
}
}
}
return null;
}
List<CEO> ceoList = new List<CEO>();
ceoList.Add(new CEO("ceo1","ceo1",5000));
ceoList.Add(new CEO("ceo2","ceo2",50001));
ceoList.Add(new CEO("ceo3", "ceo3", 50003));
int cnt = 1;
for (int i = 0; i < ceoList.Count; i++)
{
ceoList[i].vpList.Add(new VP("vp" + cnt++,"vp1",3000));
ceoList[i].vpList.Add(new VP("vp2" + cnt++,"vp2",3001));
ceoList[i].vpList.Add(new VP("vp3" + cnt++,"vp3",3002));
}
cnt = 1;
for(int i=0;i<ceoList.Count;i++)
{
for(int j=0;j<ceoList[i].vpList.Count;j++)
{
ceoList[i].vpList[j].gmList.Add(new GM("gm" + cnt++,"gm1",1000));
ceoList[i].vpList[j].gmList.Add(new GM("gm" + cnt++,"gm2",1001));
ceoList[i].vpList[j].gmList.Add(new GM("gm" + cnt++,"gm3",1002));
}
}
// IEnumerable<Employee> emp =
var q=
(from ceo in ceoList
from vp in ceo.vpList
from gm in vp.gmList
where gm.Name == "gm1"
select gm);
foreach(Employee e in q)
{
Console.WriteLine("name: " + e.Name + " alias: " + e.Alias+ " id: " + e.EmpId );
}
Can this work, using Recursion + Linq. Just worrying about the efficiency.
public class Employee
{
public string Alias { get; set; }
public string Name { get; set; }
/* other properties for employee details*/
public List<Employee> DirectReports { get; set; }
}
static IEnumerable<Employee> GetChildren(Employee e)
{
if (e.DirectReports != null)
{
foreach (var rChild in e.DirectReports.SelectMany(child => GetChildren(child)))
{
yield return rChild;
}
}
else
{
yield return e;
}
}
// Test code:
Employee gm1 = new Employee { Alias = "e1", Name = "GM1" };
Employee gm2 = new Employee { Alias = "e2", Name = "GM2" };
Employee gm3 = new Employee { Alias = "e3", Name = "GM3" };
Employee gm4 = new Employee { Alias = "e4", Name = "GM4" };
Employee vp1 = new Employee { Alias = "e5", Name = "VP1", DirectReports = new List<Employee> { gm1, gm2 } };
Employee vp2 = new Employee { Alias = "e6", Name = "VP2", DirectReports = new List<Employee> { gm3, gm4 } };
Employee ceo = new Employee { Alias = "e7", Name = "CEO", DirectReports = new List<Employee> { vp1, vp2} };
IEnumerable<Employee> result =
from e in GetChildren(ceo)
where e.Alias == "e2"
select e;
if (result == null || result.Count() == 0)
Console.WriteLine("Not found");
else
Console.WriteLine(result.First().Name);
// Output: GM2
- mahdi.oraei February 26, 2014