貢獻者:塔塔 - Jennifer
👨🏻‍🏫:interviewer
🐣:interviewee
影片連結:
英影片1

121. Best time to Buy and sell stock

👨🏻‍🏫:Hello, welcome to today’s interview. I’m Will, a software engineer. Nice to meet you.

🐣:Nice to meet you too. I’m Jennifer, a new graduate from National Cheng Kung University.

👨🏻‍🏫:Okay, let’s start our discussion. I have seen your CV, and you said that you have done a project about predicting the price of the stock.

🐣:Yes. The project is predicting the price by using machine learning skills.

👨🏻‍🏫:Sounds interesting. We can discuss the project later. Now I have a question, how to find the best time to buy and sell stock if you have several data present as the price of stock?

🐣:First, we have to find the lower number and buy the stock on that day.
Next, we have to find the highest number at the remaining data. The difference is our profit. If we can’t find the higher number in the remaining data then we return zero.

🐣:And because the price can only be positive numbers, so there might be no boundary conditions.

👨🏻‍🏫:It sounds reasonable.

🐣 : Ok, I would like to start with a direct solution. (Open doc)

class Solution{
public:
    int maxProfit(vector<int>& prices){
    int n=prices.size();
    if (n <= 1) {
            return 0; 
        }
    int max_profit=0;
    for(int i=0;i<n;i++){ 
        for(int j=i+1;j<n;j++){
            if((prices[j]-prices[i])>max_profit)
                max_profit=prices[j]-prices[i];
            }
        }
        return max_profit;
    }
}; 

🐣:And the time complexity is O(n^2), space complexity is O(1)

👨🏻‍🏫:I think there is another way to reduce time complexity, can you try it?

🐣:Umm, I think dynamic programming will be a better solution.

👨🏻‍🏫:That’s right. Go ahead for it.

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if (n <= 1) {
            return 0; 
        }

        int min_price = prices[0];
        int max_profit = 0; 

        for (int i = 1; i < n; i++) {
           
            if (prices[i] < min_price) {
                min_price = prices[i];
            }
            else if (prices[i] - min_price > max_profit) {
                max_profit = prices[i] - min_price; 
            }
        }

        return max_profit;
    }
};

👨🏻‍🏫:Okay, and that’s the end of the coding interview. Hope you have a nice day.

🐣:Thank you for your time, have a nice day!

自評:

for interviewee:

  1. "so.." 出現頻率太高
  2. 在做 Repeat 時感覺一直重複同一句話(應該要用別的方式重述題目)
  3. 應該善用註解去讓interviewer了解目前的方向(這次沒有用到)

for interviewer:

  1. 在優化演算法後應該可以再提出follow up
  2. 在第一種解法時打錯字,並沒有提出糾正
  3. 或許要在寫程式前溝通使用的語言?

☀️第一次作業 整理

這次作業參考了其他同學在作業一使用的面談技巧,也統整出我自己覺得萬用的應答及coding interview時應注意的事項

  1. 嘗試用「交流想法」而非「參加測驗」的心態應答或許可以不這麼緊張
  2. 在寫程式前用簡單例子確定題目,也順便思考極端情況再進行驗證(code tracing)
  3. 交流想法時可以用註解 coding時也可以幫助彼此釐清目前進度
  4. 不要頻繁搖動滑鼠(annoying)
  5. 若使用 google doc撰寫程式碼,可先將行距、字體大小與字形設定好,增加可讀性
  6. 作答前先溝通使用的程式語言

他評01

優:

1:59 sth comes to one's mind用法不錯。

可改進地方:

2:37 because重複太多次。可改用as / since / owing to / due to / on account of / as a result of 等等。
2:43 2:54 3:10 注意會一直出現 so. umm, or and。時間為舉例。
6:06 沒說要initialize什麼。
7:36 "a" solution, not "an"
11:04 這裡說出 "this's the one of the solutions" 直接說出你想了兩種作法(或更多),然後故意藏招不說出最好的。正常來說,你不應該把這種事情說出來。
11:33 Time complexity you could say "Big O of n to represent O(n) rather than speaking just 'n'."