簡介,蘇州微軟,目前已經電話三輪,全過,過了後能夠公費去蘇州現場面試。c++
比國外 Google的面試難度要低一些,或者說,偏重點更不同。面試
微軟你能夠只作一道面試題,思路清晰,完整,邊界狀況考慮清楚,代碼寫好就好了。算法
可是 Google 是須要你在 40 分鐘內,完美答出兩道題目,這就是區別。數組
另外,蘇州微軟是用中文的,也是容易了一些的緣由——由於用英語你確實腦子轉不過來。app
沒有給出題目,口頭說的,我整理一下:ui
判斷一個數組是否基本有序,其實就是能否經過一次簡單的交換就知足有序。spa
例子:code
1, 2, 3, 6, 5, 12
是基本有序數組,由於能夠交換 6 跟 5, 變成1, 2, 3, 5, 6, 12
1, 5, 4, 3, 6
也是基本有序數組,能夠交換 5 跟 33,2, 1, 0
就不是基本有序數組了,由於你交換一次數組並不能使此數組有序。思路不難,仔細分析一下,有序的數組數字關係是這樣: a < b < c < d < e
,而基本有序則是:a < b > c > d <e
,這樣可能很差看,簡單來講就是那兩個數字在是不知足x < y
的關係的。遞歸
能夠遍歷此數組,只要有多於一個數字是比兩邊都要大,且有一個數字是比左右兩邊要小的,那就是基本有序數組,其他的都不是。固然,你也要考慮原本這個數組就是有序的狀況。element
在面試時寫的代碼與交流草稿以下,這份代碼是不能當答案看的,只是讓朋友大家看看怎麼樣的代碼能夠過面試。建議大家本身寫寫能夠編譯過的代碼。
int array 1, 2, 3, 6, 5, 12 true i min: 3, miax: 5 1, 5, 4, 3, 6 sub array -> reverse -> true 1, 2, 3, 6, _5, 1, 5, 4, 3, 6 max: 5 min: 3 1 < min < max < least i, 1, 5, 4, 2, 8, 19, 7, 12 5, 4, 9 min: 2 max: 5 sub array -> sorted? 7 3 i: 2, 1, 2, 3, 8, 7, 6 ,5, 12 bool is_almost_sorted_array(vector<int> a) { int mem_l; bool flag = false; int l_idx = -1, r_idx = -1; On(m sub_arry) for(int i=0; i<a.size()-1; i++) { // sub array if(a[i] > a[i+1]) { int mem_l = i; int sub_min = a[mem_l]; if(mem_l+1 < a.size()) sub_min = a[mem_l+1]; while(i<a.size() && a[i] > a[i+1]) { if(flag == true) return false; i++; } /* 1, 2, 7, 4, 5, 6, 3, 8 */ /* i: 3, ->, * i: 6, * */ int sub_max = a[i-1]; if(l_idx == -1 && i - mem_l == 1) {//only one l_idx = i-1; }else if(r_idx == -1 && i - mem_l == 1) { r_idx = i; if(a[r_idx] > a[l_idx-1] && a[r_idx] < a[l_idx+1] && a[l_idx] > a[r_idx-1] && (r_idx == a.size() || a[l_idx] < a[r_idx+1])) flag = true; else return false; }else{ flag = true; //Have Changed Once if(sub_min <= a[mem_l] || i == a.size() || sub_max >= a[i]) return false; } } } return true; } test case: 1, 2, 3 => 3, 2, 1 => i:0, a[0]>a[1], mem_l:0, sub_min: 2, subm: 3 i:1, sub_min: a[2] vs 3, 1 3 i:2, 1 >= 1, 3 >=
面試反饋就是對 邊界狀況考慮不周全,能夠多增強。(是的,如上的一些判斷語句是跟面試官一塊兒過了一遍才加上的)。
第二輪面試官是貼了題目,順道解釋了一下,在這裏鍛鍊大家的英語能力,我就不給大家解釋了。
Split a string into sub-strings. Each sub-string should be no longer than the given limit. The input string contains English letters and spaces only. Do not break a word into two sub strings. Remove all spaces in the beginning or end of every sub string. Extend: append notation such as " (1 of 12)" and strings are not longer than the limit even after the notation. Length limit: 39. Input: The quick brown fox jumps over a lazy dogThe quick brown fox jumps over a lazy dogThe quick brown fox jumps over a lazy dogThe quick brown fox jumps over a lazy dogThe quick brown fox jumps over a lazy dogThe quick brown fox jumps over a lazy dogThe quick brown fox jumps over a lazy dogThe quick brown fox jumps over a lazy dogThe quick brown fox jumps over a lazy dog Result should be: The quick brown fox jumps (1 of 14) over a lazy dogThe quick (2 of 14) brown fox jumps over a lazy (3 of 14) dogThe quick brown fox jumps (4 of 14) over a lazy dogThe quick (5 of 14) brown fox jumps over a lazy (6 of 14) dogThe quick brown fox jumps (7 of 14) over a lazy dogThe quick (8 of 14) brown fox jumps over a lazy (9 of 14) dogThe quick brown fox jumps (10 of 14) over a lazy dogThe quick (11 of 14) brown fox jumps over a lazy (12 of 14) dogThe quick brown fox jumps (13 of 14) over a lazy dog (14 of 14)
這題我沒有正確的思路,由於如今我尚未想出最優解,也沒有在網上找到相似的題目,有看過的同窗能夠分享一下。難點就是,在插入每行附加信息的同時,又不能讓這行的字數超出。
我是這樣子作的:
每行先不插入信息,先正常的換行,換行完畢後,再遞歸去插入信息——由於插入信息後會由於行數進位而又須要調整一遍,如 99 變成 100.
我寫出的代碼以下:
vector<string> split_sub_strings(string input, int limit) { vector<string> res; int count = 0; for(int i=0; i<input.length(); ++i) { int mem_i = i; string current_line = ""; while(input[i] != ' ') { i++; } int word_len = i - mem_i; //能夠容納下個詞不 if(res[count].length() + word_len> limit) { res.push_back(current_line); count++; continue; }else { current_line += input.splice(mem_i, i); } while(input[i] == ' ') { i++; } } return update_info(res, limit, count); } string get_apend_info(int cnt, int sum) { return "(" + to_string(cnt) + " of " to_string(sum) + ")" } string retrieve_back_words(string s) { st for(int i=s.length(); s>0; s--) { } } // backwords: //Len 35 : The quick(1 of 14) //over a lazy dogThe quick jumps brown fox (2 of 14) // append back workds vector<string> update_info(vector<string> list_of_lines, int limit, int sum) { vector<string> res; int cnt = 0; string back_words = ""; for(auto line : list_of_lines) { string new_line = line + retrieve_back_words(back_words) + get_apend_info(cnt, sum); if(new_line.length() > limit){ for(int i=line.length(); i>0; i--) { mem_i = i; while(line[i] != ' ') { line--; } back_words = back_words + line.splice(mem_i, i) + ' '; new_line = line.splice(0, i) + get_apend_info(cnt, sum); if(new_line.length() < limit) { res.push_back(new_line); } } } } if(back_words.length == 0) { return res; }else { sum += 1; res.push_back(retrieve_back_words(back_words) return update_info(res, limit, sum); } }
這輪是純英語面試,美國的友人早晨 7 點打電話來的,對於 9 點起牀的我來講,狀態很差。
leetcode 原題 https://leetcode.com/problems/min-stack/ ,不過我沒有作過。
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Example:
MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> Returns -3. minStack.pop(); minStack.top(); --> Returns 0. minStack.getMin(); --> Returns -2.
開始,或者正常人的想法都是立刻想到用一個成員變量來存這個最小值,push 的時候能作到 O1,可是作 pop 的時候發現,這時候不清楚 pop 出去的是否是最小值以及是否是有多個最小值,這時算法就不是 O1 了。
想了一會兒後,我就想到利用 slice window(滑動窗口)的思想來作,用數組記錄區間值最小值,每次 push 不是比這個數小就不存。
例如: 12,6, 9, 7, 5, 22,29, 1
我會這樣子存: 12, 6,5,1
。
12 不管如何都是要放的,而後 6 比 12 小,存起來;9,7 都不比 6 小,不須要存——以後只要是 9,7 這兩個值,返回最小值 6 就行。
而後 5 比 6 小,存起來;22,29 大於 5,不須要存,而後最小值就是以前存起來那個 5 啦。
最後 1 比 5 小,存起來。
簡而言之,就是一個最小值列表,存起來當前區間的最小值。
pop 的時候檢查一下最小值數組是不是要 pop 的值,維護一下就行,push 跟 pop 都是 O1 了
作的時候我用的是數組,面試完後我查了下,發現能夠用抽象程度更高的 stack 來代替數組。由於我一直處理的也是數組的最後一位,跟 stack 的行爲如出一轍的。