櫻木滑倒
Sakuragi
🕵️♂️:Interviewer
🕶️:Interviewee
模擬面試錄影-1 (漢)
模擬面試錄影-2 (漢)
模擬面試錄影-3 (英)
🕵️♂️: 你好,我是Jack,我是任職於Banana公司的面試官。
🕵️♂️: 我給你Google Doc的連結之後就可以開始了。
🕵️♂️: 在電腦圖學中,無論是在構建渲染環境還是設計遊戲,都經常需要計算物體之間的距離,而這種計算往往涉及平方根的運算。
🕵️♂️:假設今天給你一個介於 到 的數值,並要求在不使用任何函式庫的情況下,我們想求出的平方根。
🕵️♂️:請問你對題目有疑問嗎?
🕶️:有,我想請問他的平方根不是整數的情況下如何表示答案?
🕵️♂️:往下取整數就可以了。
🕶️:好的,爲了確認我對題目的理解,我想用一個例子説明。
輸入: 10
輸出: 3
🕶️:請問我有理解錯題目嗎?
🕵️♂️:沒有,你可以開始寫你的程式了。
🕶️:在寫程式之前,我想先講一下我的想法。
🕶️:最直接的解法是使用一個迴圈,從輸入的值開始依次遞減,利用decision判斷當前數字的平方是否小於或等於輸入值。如果符合這個條件,則該數字就是輸入值的平方根。
🕵️♂️:你用一個迴圈把所有可能的答案都判斷一次是可以解決我們的題目。但以實務層面來説,輸入資料十分大時,程式的效能會很差。請問你有其他更優秀的解法嗎?
🕶️:確實,之前的解法時間複雜度是 。
🕶️:我的想法是利用二分搜尋法來解,這樣可以大幅度降低迴圈次數,將時間複雜度降到 。具體來說,二分搜尋法的思路是每次將搜索範圍縮小一半,直到找到接近或等於輸入值平方根的整數。
🕶️:一開始判斷輸入是0或1的狀況,接下來開始實作二分搜尋法,最後再用一個判斷式來處理往下取整數的情況。
🕵️♂️:非常好,如果我們公司今天需要開發一款遊戲,且需要處理大量的平方根運算,而數值範圍是有限的,請問有沒有更快的解決方案呢?
🕶️:如果數值範圍是有限的,我們可以使用雜湊表來最佳化演算法。將系統中可能用到的平方根事先計算並存入雜湊表中,這樣在需要計算平方根時就可以直接查表,從而實現 的時間複雜度。
🕵️♂️:非常好,櫻木滑倒先生。我們會再寄第二封的面試通知到你的信箱,祝你好運。
🕶️:非常感謝你,Jack先生。
public
這樣的關鍵字 (keyword) 唸出來,專注在程式碼的本體decision
的命名不精準,劍橋詞典的解釋: "a choice that you make about something after thinking about several possibilities"🕵️♂️: 你好,我是Jackson,我是任職於Grape公司的面試官。
🕵️♂️: 我給你Google Doc的連結之後就可以開始了。
🕵️♂️: 巴斯卡三角形能計算二項式係數,對於二項式展開至關重要。然後二項式展開在演算法的設計和機率統計中也有廣泛應用。
🕵️♂️:假設給定一個介於 0 到 33 之間的數字 rowIndex,表示巴斯卡三角形的第 rowIndex 層,最上方為第 0 層,我們的目標是求出該層的所有二項式係數。
🕵️♂️:請問你對題目有疑問嗎?
🕶️:沒有,但是爲了確認我對題目的理解,我想用一個例子説明。
輸入: rowIndex = 3
輸出: [1,3,3,1]
🕶️:請問我對於題目的理解上有任何錯誤嗎?
🕵️♂️:沒有,你可以開始寫你的程式了。
🕶️:在寫程式之前,我想先講一下我的想法。
🕶️:我的想法是先判斷初始條件,當輸入為 0 或 1 時,直接返回對應的結果。如果輸入大於 1,則使用雙重迴圈來計算巴斯卡三角形的值。第一層迴圈負責根據目標層數逐層更新,每次計算時創建一個 temp 來存放新一層的值。第二層迴圈負責根據目前的層數,兩兩相加計算巴斯卡三角形的係數,並將結果存入 temp。雙重迴圈完成後,將計算結果賦值給 answer。
🕶️:這樣的解法由於使用了雙重迴圈,因此時間複雜度為 。同時,使用了 vector,最終會存放 rowIndex + 1 個元素,其他變數皆為常數級別,因此空間複雜度為 。
下方程式碼的迴圈並非「對等」,不是「雙」。改稱「二層迴圈」。
🕵️♂️:做得非常好。但有個小問題,以實務層面來説,輸入資料十分龐大時,程式的效能會很差。請問你可以提出更優秀的演算法嗎?
🕶️:沒錯,之前的解法時間複雜度與 rowIndex 的平方成正比。
🕶️:我們可以直接利用二項式係數的性質來計算每一行的元素,以達到 的時間複雜度。
🕶️:具體的公式如下:
🕶️:巴斯卡三角形第 n 行的每個元素是依據二項式係數直接計算出來的。
🕶️:我們這邊也使用到二項式係數的遞迴公式來計算目標層的每個元素:
🕶️:這個演算法一樣使用了一個大小為 rowIndex + 1 的 vector 來存儲結果,因此空間複雜度仍然是 O(rowIndex)。
🕵️♂️:非常好,櫻木滑倒先生。我們會再寄第二封的面試通知到你的信箱,祝你好運。
🕶️:十分感謝你,Jackson先生。
針對 interviewer 的檢討:
針對 interviewee 的檢討:
🕵️♂️: Hello, I am Johnson, the interviewer from Watermelon Company.
🕵️♂️: I'll send you the Google Doc link, and you can start once you're ready.
🕵️♂️: A stack is essential for managing function calls, expression evaluation, implementing undo features, and efficiently handling depth-first search (DFS) in graph or path exploration.
🕵️♂️: To design the MinStack, you need to ensure that all operations—push, pop, top, and getMin—run in constant time, . You'll be working within the constraints of integer values between and , so your implementation should handle those boundaries. Additionally, you can assume that pop, top, and getMin will always be called on non-empty stacks, which simplifies the need for error handling around empty stacks. With up to 30,000 method calls, it's essential that your design remains efficient under heavy use.
🕵️♂️: Do you have any questions about the problem?
🕶️: Yes. So, does a MinStack simply have an extra function to track the minimum value in the stack compared to a regular stack?
🕵️♂️: You can say that again.
🕶️: One more thing, can I use the stack library to implement the code?
🕵️♂️: Yes, you can use the stack library.
🕶️: To make sure I understand the problem correctly, I'd like to explain it with an example.
Input: ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]]
Output: [null,null,null,null,-3,null,0,-2]
🕶️: Am I understanding the problem correctly?
🕵️♂️: Yes, you can start writing your code.
🕶️: Before writing the code, I'd like to explain my approach.
🕶️: My approach is straightforward. The pop, top, and getMin functions work similarly to a regular stack. The key difference is in the MinStack constructor: I use a stack where each element stores both its value and the minimum value of the stack up to that point.
🕶️: This solution is very simple, so the time complexity of each operation is simply .
🕵️♂️: Excellent, Mr. Sakuragi! This is quite challenging, but it seems you’re the expert we’ve been searching for. We’ll send a follow-up interview notification to your email. Best of luck!
🕶️: Thank you very much, Mr. Johnson.
Feedback for the interviewer:
Feedback for the interviewee: