中軟面試題-最新
中軟的面試比較經典,也比較嚴格,一般有四輪,類似于微軟的面試。中軟面過以后,根據項目組,會推到美國微軟那邊運用live meeting & con-call 再面一次。以下是我的面試題及個人的小分析,拿出來和大家share一下。希望更多的人能過這個坎。如有什么問題,可以一起交流。直接進入主題:
1. English communication. (sale yourself, project information, your interesting,and how to deal with problem you encounter etc.)
2. the using of key words "new".
Regarding this problem, you can refer to my other blog with this path: new . or you can get more information from internet.
3.Write a method which can remove the same unit from a Array which has been sorted.
{
if (a.Length==0)
{
return a;
}
List <int> Result = new List<int>();
int i , j;
i = j = 0;
int temp = a[0];
while (i < a.Length)
{
if (a[i] != temp)
{
j++;
temp = a[i];
Result.Add(temp);
}
i++;
}
// convert List to Array
//......
return Result;
}
4. Judge Triangle and write test case for your code.
public int Triangle(int a, int b, int c)
{
if (a <= 0 || b <= 0 || c <= 0)
{
return 4;
}
int [] arry = new int [3] { a, b, c };
Array.Sort(arry);
int min, mid, max;
min = arry[0];
mid = arry[1];
max = arry[2];
if (max-mid<min) // 注意:用這個去判斷是否能構成三角形
{
return 4; //不能構成三角形
}
if (min == max)
{
return 1; //等邊
}
else if ( mid==min || mid == max)
{
return 2; // 等腰
}
else
return 3; // 其他
}
在這里,我最想提的就是這一題了,因為,我們知道,判斷三角形,我們常用 兩邊之和大于第三邊。但是,在這里,就不能用了。例如: a= int.MaxValue, b=int.MaxValue, c=int.MaxValue, 這時候三邊肯定能構成三角形,但是,a+b 的和已經超過了int 型的最大值。造成內存溢出。 有人說,內存溢出 就是 0 或者負數,用這個不就可以判斷嗎?這個還真不行。你可以多寫幾個test case 試試。
Test case:
其他的普通的case,我就不寫了,在這里就強調一下,邊界值的問題(也就是常說的 臨界值)。int.maxvalue, 0 etc.
5.Reverse string.
{
char temp;
int len = end - start;
int i=0;
while(i<len/2)
{
temp = str[start+i];
str[start +i] = str[end -i - 1];
str[end-i-1] = temp;
i++;
}
return str;
}
public string Reverse(string str)
{
if (String.IsNullOrEmpty(str))
{
return null;
}
char [] objstr = str.ToCharArray(); ;
int length=objstr.Length;
objstr = Convent(objstr,0,length);
int i = 0;
int start=0,end=0;
while (i < length)
{
if (objstr[i] == ' '||i==length-1)
{
if (i == length - 1)
{
end = i + 1;
}
else
{
end = i;
}
objstr = Convent(objstr, start, end);
start = end+1;
}
i++;
}
return new string(objstr);
}
6. Find the most width level in a tree and return the count of level, if there are many one, just return the nearest level. (it can be found in the internet)
int Width(BinTree T)
{
int static n[M];//向量存放各層結點數
int static i=1;
int static max=0;//最大寬度
if(T)
{
if(i==1) //若是訪問根結點
{
n[i]++; //第1層加1
i++; //到第2層
if(T->lchild)//若有左孩子則該層加1
n[i]++;
if(T->rchild)//若有右孩子則該層加1
n[i]++;
}
else
{ //訪問子樹結點
i++; //下一層結點數
if(T->lchild)
n[i]++;
if(T->rchild)
n[i]++;
}
if(max<n[i])max=n[i];//取出最大值
Width(T->lchild);//遍歷左子樹
i--; //往上退一層
Width(T->rchild);//遍歷右子樹
}
return max;
}//算法結束
7. Implement the function: Int ConvertToInt(string num)
{
int result=0;
int temp=0;
if (!string.IsNullOrEmpty(num))
{
if (IsInteger(num))
{
for (int i = 0; i < num.Length; i++)
{
temp = result;
result = result * 10 + ((int)num[i] - 48); //0 的Asscall碼 是48
if (temp == result)
continue;
}
if (temp != result)
{
throw new Exception("overflow");
}
}
}
return result;
}
// 判斷字符串是否是整數。
public bool IsInteger(string strIn)
{
bool bolResult = true;
if (strIn == "")
{
bolResult = false;
}
else
{
foreach (char Char in strIn)
{
if (char.IsNumber(Char))
continue;
else
{
bolResult = false;
break;
}
}
}
return bolResult;
}
關于上面的判斷字符串里轉換后是否是整數,還有其他的方法:
{
char[] ch = new char[str.Length];
ch = str.ToCharArray();
for (int i = 0; i < ch.Length; i++)
{
if (ch[i] < 48 || ch[i] > 57)
return false;
}
return true;
}
8. Quick sort. (you can get it from internet)
{
if (begin < 0 || end < 0 || begin > end)
return;
int left = begin, right = end, temp;
temp = array[left];
while (right != left)
{
while (temp < array[right] && right > left)
right--;
if (right > left)
{
array[left] = array[right];
left++;
}
while (temp > array[left] && right > left)
left++;
if (right > left)
{
array[right] = array[left];
right--;
}
}
array[right] = temp;
Quicksort(array, right + 1, end);
Quicksort(array, begin, right - 1);
}
Ok, that is all.
那次面試比較久,接近四個小時。后面被推到微軟那邊面試,面試題,詳見后面解析。