Careercup - Microsoft面试题 - 4840369632051200

2014-05-10 07:06

题目链接

原题:

Suppose you have a collection of collection 

Eg : CEO-> Vps-> GMs ->.. 

CEO will contain collection of VP's, VP's will have collection of GM's and so on. 

Suppose you need to find a particular GM is the alias is given.    Write a linq query to get the employee details if the employee alias is given. 

Hint : Use Recursion + Linq

题目:如果CEO手下有很多总监,总监手下有很多总经理,依此类推。那么通过一个名字和一个头衔,要如何找到符合条件的人?用递归和LINQ实现。

解法:这明显是C#问题吧。C#我勉强懂点基本语法,LINQ则完全不会,所以我只能用递归了。

代码:

 1 // http://www.careercup.com/question?id=4840369632051200

 2 // I believe this is not a typical interview question. You must've told them you know LINQ, before you're given this one.

 3 // I've never learnt LINQ before, only basic knowledge of C# is not enough to solve this problem.

 4 using System;

 5 using System.Collections.Generic;

 6 using System.Linq;

 7 using System.Text;

 8 using System.Threading.Tasks;

 9 

10 namespace CSharp

11 {

12     class Person

13     {

14         public string title;

15         public string alias;

16         public List<Person> subordinate;

17 

18         public Person(string title, string alias)

19         {

20             this.title = title;

21             this.alias = alias;

22             subordinate = new List<Person>();

23         }

24     }

25 

26     class Solution

27     {

28         Person search(string title, string alias, Person source) {

29             if (source.title == title) {

30                 return source.alias == alias ? source : null;

31             }

32 

33             Person result;

34             foreach(Person sub_source in source.subordinate)

35             {

36                 result = search(title, alias, sub_source);

37                 if (result != null)

38                 {

39                     return result;

40                 }

41             }

42 

43             return null;

44         }

45     }

46 }

 

你可能感兴趣的:(Microsoft)