Microsoft Interview Question for Software Engineer / Developers Software Development Managers


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MS_Question
{
    class Program
    {
        CEO ceo;
        Program()
        {
            Employee w1 = new Employee(); w1.name = "w1";
            Employee w2 = new Employee(); w1.name = "w2";
            Employee w3 = new Employee(); w1.name = "w3";
            Employee w4 = new Employee(); w1.name = "w4";
            Employee w5 = new Employee(); w1.name = "w5";
            Employee w6 = new Employee(); w1.name = "w6";
            Employee w7 = new Employee(); w1.name = "w7";
            Employee w8 = new Employee(); w1.name = "w8";

            GM gm1 = new GM(); gm1.name = "gm1";
            gm1.workers.Add(w1); gm1.workers.Add(w2);
            GM gm2 = new GM(); gm2.name = "gm2";
            gm2.workers.Add(w3); gm2.workers.Add(w4);
            GM gm3 = new GM(); gm3.name = "gm3";
            gm3.workers.Add(w5); gm3.workers.Add(w6);
            GM gm4 = new GM(); gm4.name = "gm4";
            gm4.workers.Add(w7); gm4.workers.Add(w8);

            VP vp1 = new VP(); vp1.name = "vp1";
            vp1.mgs.Add(gm1); vp1.mgs.Add(gm2);
            VP vp2 = new VP(); vp2.name = "vp2";
            vp2.mgs.Add(gm3); vp2.mgs.Add(gm4);

            CEO ceo1 = new CEO(); ceo1.name = "ceo1";
            ceo1.vps.Add(vp1); ceo1.vps.Add(vp2);
            this.ceo = ceo1;
        }

        public Human findWorker(Human h, String name){
            if (h is CEO)
            {
                if(h.name.Equals(name))
                    return h;
                else
                {
                    
                    IEnumerable<VP> res = ((CEO)h).vps.Select(i => i).Where( i => i.name.Equals(name));
                    foreach (VP vp in res)
                    {
                        return vp;
                    }
                    foreach (VP v in ((CEO)h).vps)
                    {
                        return findWorker(v, name);
                    }
                }
                    
            }
            if (h is VP)
            {
                IEnumerable<GM> res = ((VP)h).mgs.Select(i => i).Where(i => i.name.Equals(name));
                foreach (GM mg in res)
                {
                    return mg;
                }
                foreach (GM m in ((VP)h).mgs)
                {
                    return findWorker(m, name);
                }
            }

            if (h is GM)
            {
                IEnumerable<Employee> res = ((GM)h).workers.Select(i => i).Where(i => i.name.Equals(name));
                foreach(Employee w in res){
                    return w;
                }
            }

            return null;
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            Human h = p.findWorker(p.ceo, "ceo2");
            if (h != null)
            {
                Console.WriteLine(h.name + " Exists.");
            }
            else
            {
                Console.WriteLine("No such person exists.");
            }
            Console.ReadKey();
        }
    }
    class Human{
        public string name="";
    }
    class CEO:Human
    {
        public List<VP> vps = new List<VP>();
    }
    class VP:Human
    {
        public List<GM> mgs = new List<GM>();
    }
    class GM:Human
    {
        public List<Employee> workers = new List<Employee>();
    }
    class Employee : Human
    {

    }
}

- mahdi.oraei February 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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->...

- RKD February 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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;
        }
    }
    
}

- mahdi.oraei February 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
        }

- eile February 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public string GetGMByAlias(string alias)
{
if (string.IsNullOrWhiteSpace(alias))
return null;
var gms = (from v in listOFVps
from g in v.listOfGMs
where g.Alias.Equals(alias)
select g).FirstOrDefault<GMs>();
return gms.ToString();
}

- Biruk March 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 );
}

- praveen April 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Eric Lei April 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string gmtofind;
//Assign GM

CEO.Vps.SelectMany(vp=>Vp.GMs).where(gm=>gm.alias == gmtofind).firstordefault();

- Shyam October 29, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More