🧔:interviewer 👶:interviewee
🧔: You're given two sorted linked lists, and you need to merge them into one list with the sorted order.
👶: Ok, Let me confirm the question. I need to make two sorted list into one list with ascending or decending order. Depend on the order of the given list. Is that correct?
🧔: Yes, and you can assume they are in ascending order.
👶: OK. So, if I have two lists with 1,2,3 and 2,3,4 respectively the answer should be 1,2,2,3,3,4
🧔: Exactly, you can start you code.
👶: In my intuition, I will approch it with iterate through every node in two list by comparing each other everytime.
👶: So, Since we iterate though each node in worst case, the time complexity should be .
🧔: It looking great. But the control statement may cause control hazard. Can you reduce the if statement?
👶: I will start from reducing the special case. So the specail case is when …
🧔: OK, I think it great enough. Since we don't have too much time left. Let's end up here. Thank you for your time.
Interviewer
Interviewee
🧔: 你會得到一個排序好的鏈結串列,你需要把相同數值的節點移除。
👶: 我確認一下問題,我有一個排序好的 Linked list,如果遇到重複的資料就刪掉,只留一個在 Linked list 裡面。
🧔: 對。
👶: 也就是說,如果我有的 Linked list 長 [1,1,3,4,4] 經過我的 function 後會變成 [1,3,4],請問是這樣嗎?
🧔: 沒錯。
👶: 我的直覺是直接走訪每一個節點,然後拿當前走訪到的節點向後比較,如果相同就直接移除。
👶: 因為每個節點都需要訪問過,因此複雜度為
🧔: 看起來沒問題,那如果我連重複的節點本身也要刪除呢?(Medium Leetcode 82.)
👶: 這樣就要刪除 cur 指向的節點,因此我們需要一個 flag 判斷是否有進入 while 迴圈,同時要將 cur 改為指標的指標。
Interviewer
Interviewee
🧔: 今天會要求你實作一個 LRU cache,總共有 3 個 function 需要你實作。lRUCacheCreate、lRUCacheGet、lRUCachePut。lRUCacheCreate 的功能是初始化 cache 大小,由於我們的 cache 皆是 key、value 成對的,因此 lRUCacheGet 會給定一個數值 key,你需要回傳相對應的 value,最後 lRUCachePut 會給定 key、value,如果資料存在 cache 中就更新 value,如果不在 cache 則加入 cache。
👶: 假如說 cache 的大小是 2,當我先做兩次 put 操作 put(1,1), put(2,2),這時候如果再次進行 put 操作 put(3,3),這時候被移出的資料為 1,1,因為他是最久沒被使用到的資料。
🧔: 對,你可以假設 cache 的 capacity 至少為 1。
👶: 我直覺的作法是使用一個鏈結串列,如果有對資料操作就將資料放到鏈結串列的開頭,這樣當 cache 放滿資料時,串列的最尾端的節點就會是需要被移除的節點。
👶: 最差的情況下複雜度為 ,因為在比對 key 的時候需要一個節點一個節點慢慢往後比較。
🧔: 由於我們要實作 cache,複雜度還是希望可以控制在 ,請問你會如何修改你的程式。
👶: 我會使用 hash 的方式,同時使用環狀鏈結的方式,這樣查找與移除節點都可以在 完成。
Interviewer
Interviewee
move_to_head
之後應該要將 value 進行修改。在後續的檢查過程也沒有檢查到。優點
可改進的地方
interviewer
interviewee