貢獻者: 沙西米-Sashimi
模擬面試錄影: 1, 2, 3👩🦰: interviewer
👶: interviewee
👩🦰: 同學妳好,希望妳能實作一個function,輸入一個整數後,判斷它是否屬於迴文數。
👩🦰: 對題目有什麼問題嗎?
👶: 請問迴數指的是正反兩面讀出來的數字都一樣嗎?
👩🦰: 是的。
👶: 那麼如果是負數的情況,需要將正負號考慮進去嗎?
👶: 舉例來說,如果
👶: 請問我理解的正確嗎?
👩🦰: 正確。
👶: 請問整數的範圍是?
👩🦰: <= x <= ,可以開始寫程式了。
👶: 在開始寫程式之前,我想先說一下我的想法。
👶: 我目前直觀的想法是先將整數轉換為字串,在字串頭尾各設一個指標,相互比對,如果相同就向中間位置靠近。
👩🦰: 好的,請開始實作。
👶:
👩🦰: 使用整數轉換字串確實可以解決我們的問題,但是使用字串花費的記憶體比整數大很多,是否有更好的作法?
👶: 嗯…好的,我想一下…
👶: 如果將題目給的整數拆成兩個部分,反轉其中一個部分,再比較兩個部分是否相等,這個思路是否能節省更多記憶體?
👩🦰: 可以從這個方向寫看看。
👶: 好的。
可以改進的地方:
👩🦰: In this question, you are given the heads of two sorted linked lists list1 and list2.
👩🦰: You need to merge the two lists into one sorted list.
👩🦰: The list should be made by splicing together the nodes of the first two lists.
👶: Okay, I would like to use an example to explain my thought about this question.
👶: If there are two inputs: list1 and list2
👶: Am I correct?
👩🦰: Right. You can start coding.
👶: Before coding, I would like to explain my approach about this question.
👶: I will compare the first elements in list1 and list2. If the one in list1 is smaller than the one in list2, and then I will put the first element in list1 into the output list.
👶: Then compare the second elements in list1 and the first elements in list2. Put the smaller on into the output list, vice versa.
👶: If both elements are equal, and then put them into the output list.
👶: I'm curious about if there is a limit on number of elements in lists?
👩🦰: It's from 0 to 50. You can start coding.
👶: I create a dummy node to point to the head of the output list.
👶: Compare the elements of both lists, and put the smaller one into the output list.
👩🦰: Okay now, please do a trace to see if it works, just take the example upside.
👶: Well, there is a problem, when the list2 is point to NULL, list1 is still point to the node with 4 inside.
👶: There is no way to do the comparison between NULL and 4.
👩🦰: Also, if the number of elements of both lists are different. How do you change your code?
👶: Okay, um… If one list is over, and then directly connect the other list to the end of output list.
👶:
可以改進的地方:
👩🦰: 這裡想請妳用linked list實作看看加法。
👩🦰: 首先,會給你兩個用來表示非負整數的non-empty的linked list,這些整數以相反的順序儲存,將兩個數相加,以linked list的形式回傳總和。
👶: 我想說一下我對題目的理解:
👶: 舉例來說
👩🦰: output的部分維持相反的順序儲存,因此需要進行修改。
👶: 所以應該是
👩🦰: 是的,可以開始寫程式了。
👶: 在開始寫程式之前,我想先說明一下我的實作方案。
👶: 因為linked list是以相反的方式儲存,因此由head開始向後的順序,也代表個是百千的順序。
👶: 我會從兩個linked list的第一個element開始相加,並給定一個紀錄進位的變數carry,依序向後進行運算,每完成一位數,就在output list中添加一個新的node,直到計算完成。
👩🦰: 聽起來沒問題,可以開始實作了。
👶:
👩🦰: 妳現在的function是假設兩個list的長度相同,但是在進行加法時,相加的兩個數未必都是相同的位數,請針對這個部分進行修改。
👶: 好的。
可以改進的地方:
優點:
咬字清晰,說明速度適中
interviewer說明程式碼的部分蠻清楚的
interviewee有提出問題讓interviewer可以有很好討論問題的互動
缺點:
打程式的過程跟解釋可以更貼合,可以多練習在解釋的同時一步一步地把程式碼順便就打出來