天天看點

去哪兒java面試_《去哪兒》JAVA研發筆試+一面

背景不多說了,入正題,先說下筆試吧,研發崗位總共三道程式設計題,總的來說不是特别難,但要求要紙上寫代碼,是以寫代碼前先三思下,可以在試卷上寫僞代碼思路(答卷分開),後在答卷上寫題,切記:一定别忘記注釋。

筆試題:

一、部分有序旋轉數組查找問題。

有一遞增數組可向右移動,如:{1,2,3,4,5}向右移動後可為:{3,4,5,1,2},現在類似的數組中想找一個固定值,若存在則傳回對應下标,若不存在則傳回-1。需要考慮算法效率。

題解:很容易的就想到二分查找,是不是?但問題來了:數組有可能是由兩部分遞增的數組組成的,這樣子就不能用二分查找法了。等等,這兩部分有特别發現了沒?數組第一個元素和最後一個比較,如果第一個比最後一個大,那麼數組就是旋轉了,此時很容易就想到先将原數組分割成兩個遞增數組再分别二分查找。

public int indexInTurnedArray(int[] array, int index) {//find out where the target int is

int tmp = -1;if(null == array || 0 == array.length) {//error input

tmp = -1;

}else{int bound = findArrayBound(array);//get the boundary of the array, which means the bigest one in array

if(index == array[bound]) {//the target int is the boundary

tmp = bound + 1;

}else{//the target int should in the left side of the boundary, or the right side//anyside should return the index of the array, else return -1//so, if none exist in both sides, the result will be -1//if the target is match to the first one of the array, the 0 should be the reslut

tmp = 1 + binarySearch(array, 0, bound - 1, index) + binarySearch(array, bound + 1, array.length -1, index);

}

}returntmp;

}

public int findArrayBound(int[] array) {if(array[0] < array[array.length - 1]) {//the array is a sequnced array, small to big...

return array.length - 1;

}else{int tmp = 0;//begin is used for index the last one in the bigger side//end is the first one in the smaller side

int begin = 0, end = array.length - 1;while(begin < end) {//when the begin = end ,the circle will exist, means find the boundary

if(1 == end - begin) {//found the boundary

tmp = end - 1;break;

}

tmp= (begin + end) / 2;if(array[tmp] >= array[begin]) {//the middle one is bigger than begin

begin =tmp;

}else if(array[tmp] <= array[end]) {//the middle one is small than end

end =tmp;

}

}returntmp;

}

}public int binarySearch(int[] array, int begin, int end, int index) {//binary research function

if(begin ==end) {return index == array[begin] ? begin : -1;

}else{int middle = (begin + end) / 2;if(index ==array[middle]) {returnmiddle;

}else if(index

}else{return binarySearch(array, middle + 1, end, index);

}

}

}

二、字元串解釋

對輸入的字元串,如:a2b3c4解密,解密結果應該為aabbbcccc。

題解:比較簡單的一題字元操作,考查ASCII碼運用及,字元串的相關知識。

stringstringDecode(tring s)

{char* c = const_cast(s.c_str());//将輸入字元串強制轉換成字元

String tmp;while (*c != '\0')

{char a = 0;int j = -1;if ((*c>='A' && *c <= 'Z') || (*c >= 'a'&&*c<='z'))//判定是否是字母

{

a= *c;

c++;

}if (*c != '\0' && *c >= '0' && *c <= '9')//判定是否是數字

{while (*c != '\0' && *c >= '0' && *c <= '9')//将所有數字轉換成int

{if (-1 ==j)

{

j= (int)*c-48;

}else{

j= j * 10 + ((int)*c-48);

}

c++;

}

}if (a != 0)//若第一個是字母,則輸出

{if (-1 ==j)

{

tmp+=a;

}else{for (int i = 0; i < j; i++)

{

tmp+= a;//疊加之前的字母個數

}

}

}

}returntmp;

}

三、多元數組操作

有一酒店某段時間内價格是由三元組确定的,如:第一天至第30天的價格是300元,則三元組為:[1,30,300]。後來出現的三元組的價格可以覆寫之前三元組的價格,如再出現[20, 50, 100]則其價格為:[1,19, 300], [20, 50, 100]。

題解:題目即為輸入是二維數組,每行第一個元素是起始日期,第二個元素是截至日期,第三個元素為此段時間内價格。我很容易的想到了上萬數亂序查找哪個數不存在的問題,即:建立一個較目标大點的一維數組(此處大小為366,一年不超過366天),若輸入的三元組中規定了日期的價格,則分别将對應下标的數組中的值改為價格,後來再有重複的話将之前的價格覆寫即可。最後在周遊數組,找出非0的部分,值相同的一段将起始下标、終止下标、價格作為新的三元組輸出。

1 public void hotelPrice(int[][] array) {2 int beginDay = 365, endDay = 0; //store the begin and end day of the input

3 int[] price = new int[366]; //the price must in a year

4 int tmp = 0;5 for(int i = 0; i < array.length; i++) { //the 3-element-array number

6 if(beginDay > array[i][0]) { //price begin day

7 beginDay = array[i][0];8 }9 if(endDay < array[i][1]) { //price end day

10 endDay = array[i][1];11 }12 tmp = array[i][0];13 while(tmp <= array[i][1]) {14 price[tmp++] = array[i][2]; //pice

15 }16 }17 int i =beginDay;18 while(i <= endDay) {//print result

19 if(0 ==price[i]) {20 i++;21 continue;22 } else{23 int begin =i;24 int tmpV =price[i];25 while(i < 366 && price[++i] == tmpV) {//find the same price day

26 continue;27 }28 int end = i - 1;29 System.out.println("[" + begin + " , " + end + " , " + tmpV + "]");//print

30 }31 }32 }

面試:

主要問了些關于履歷和筆試中的細節問題。

一、由于之前我在銳捷實習過4個月,問了些關于銳捷實習過程中我做的一個用robotframework搭建的自動化測試平台的東西,這部分聊的比較HIGH,和面試官聊了半天算是科普了許多無線方面的知識吧,還說了一些我在項目中遇到的問題和相應的解決思路和過程。

二、socket和http的差別。

http是超文本傳輸協定,主要是用來在應用程式層面封裝web資料的一個協定,封裝完後再交由TCP封裝,IP封裝,最後由底層網絡送出封包傳遞;

socket可以分為IP層的socket和TPC層的socket,其主要功能是為了友善程式員更好的調用協定棧完成相應工作,簡單的說就是對協定棧封裝提供一套API給開發者用。

故,一個是協定;另一個隻是一系列的接口API。

三、什麼是線程安全。

(從度娘搬運來的)線程安全就是多線程通路時,采用了加鎖機制,當一個線程通路該類的某個資料時,進行保護,其它線程不能進行通路直到該線程讀取完。不會出現資料不一緻或污染。線程不安全就是不提供資料通路保護,有可能出現多個線程先後更改資料造成所得到的資料是髒資料。

四、Spring主要作用

(從CSDN上搬運過來,http://blog.csdn.net/dyllove98/article/details/8586312)

五、資料庫外鍵

外鍵主要是用于兩個或多個表關聯時,關聯的關鍵字即是外鍵。

由于當時問到銳捷項目後,問了許多python語言類的東西,本人實在太菜未系統學過,是以基本答的很亂,最後妥妥的跪了。歡迎大家來拍磚和補充。