貢獻者: 布撥(Pawmi)
影片-1
影片-2
影片-3
🐯: interviewer
🦝 : interviewee
🐯: 布撥先生你好,歡迎來到本公司,本次面試為coding interview ,我們有一些問題想與你討論,在過程中如果對問題有任何的疑問,都可以提出。如果你準備好就可以開始了。
🦝 : 好的,我可以開始了。
🐯: 首先是關於數據合併的問題,在處理公司後台系統的客戶數據時,我們經常需要將不同子系統中的相同類型數據進行整併,並希望在整併完成後,數據依然維持順序或逆序排序。假設我們有兩組來自不同子系統的整數陣列 nums1 與 nums2, 兩者都為遞增排序,其中nums1有m個整數,而nums2有n個整數,我們需要將兩個陣列進行合併,並維持它們的遞增排序,請問你會如何解決此問題呢。
🐯: 補充說明,我們希望最終合併的結果直接儲存在nums1陣列中,因此我們已經將nums1陣列擴增至能含括所有數據的長度,即 m + n ,擴增的部分由整數0填充。
🦝 : 好的,我想先舉一例子,來確認我對題目的理解。
🦝 : 請問我的舉例正確嗎?
🐯: 正確,你可以提出解決方案了。
🦝 : 好,那我會透過python來撰寫我的程式碼。因為num1陣列已經先擴充到目標的長度了,所以我會先將nums2的數據先複製到nums1的後n個索引位置,然後再對整個nums1陣列進行排序,使nums1儲存我們期望的結果。
🐯: 你的程式碼很精簡,且也能夠正確的達到我們的需求,但一開始我有提到,nums1與nums2兩個陣列在合併前已經完成了遞增排序,你的方案是否能將這個特性進行利用呢?
🦝 : 了解,我先前的方法忽略了這個重要的性質,因為陣列已經排序過了,所以可以透過兩個索引值逐步比對兩陣列中的數值大小,並透過另一個索引值將較大的數值填入num1中正確的位置。
🦝 : 這樣的改進可以避免再次進行排序,原本排序的時間複雜度為O(NlogN),現在我們只需要對兩陣列逐一進行比對,解決方案的時間複雜度進一步改進到O(N)
🐯 : 接下來同樣是關於陣列數據的處理,假設我們有一個整數陣列nums,要將它向右旋轉k步,請問你會如何設計解決方案呢?
🦝 : 我想先確認一下,向右旋轉的定義是什麼呢?
🐯 : 類似讓數據在固定的資料結構裡循環的概念,假設我想讓陣列向右旋轉1步,完成後陣列中的數值都會向後位移一格,而最後一個數值則會回到陣列的開頭。
🦝 : 我了解了, 讓我舉例來說。
🦝 : 我這樣的舉例是正確的嗎?
🐯 : 對的,你可以開始規劃解決方案了。
🦝 : 我想透過複製陣列的方式解決這個問題,首先我先複製nums陣列,將nums陣列長度定義為l ,並將l減掉k個的數值依序append到複製的陣列後面,最終複製陣列的後面l個數值即為我們的目標值,需要特別注意當k等於l時,代表陣列旋轉循環了一圈,因此我們在實際操作時,其實只要旋轉k除以l的餘數即可。
🐯 : 你可以再提出一組範例來驗證方法的有效性嗎?
🦝 : 好的,我們來測試當旋轉步數多於陣列長度時的狀況。
🐯 : 的確都能夠正確的達到需求。接下來我們加上一些額外的設定,來進一步討論程式效率改進的可行性;如果我們的解決方案需要佈署在記憶體空間極度有限的裝置上,要求in-place的解決該問題,你會如何改進你的方案呢?
🦝 : 原本的方法中複製了nums陣列並透過append來逐步添加元素,但只要我們事先計算出陣列中的那些元素在旋轉之後會被位移到前半段,那些部分會位移到後半段,我們就能夠透過反轉的方式,得到我們的目標。
🐯 : For the next part, I'd like to discuss in English with you.
🐯 : When dealing with data, we often need to transform it into data structures that are more suitable for retrieval. A height-balanced binary search tree is one such structure. Given an integer array nums where the elements are sorted in ascending order, you need to convert it to a height-balanced binary search tree.
🦝 : How should I represent the structure of the tree in my solution?
🐯 : Your function needs to be able to convert the array data into a tree structure and return the root of the tree. The interface for using your designed function will output values using the BFS algorithm.
🐯 : You can use this structure for implementation.
🦝 : For example:
For this input, I need to create a height-balanced binary search tree like this
🦝 : Is my understanding correct?
🐯 : Correct, you can start designing your solution now.
🦝 : I'd like to solve this problem using recursion. Since the array nums is already sorted, for a height-balanced BST, the value of its root is the median of all the node values. So, we can create a build function that takes the middle index value (half of the array length) of nums as the node value and recursively call it to build the left subtree from the first half of nums and the right subtree from the second half of nums. This recursion continues until the input array length for the build function becomes 0. Now, let's move on to the code implementation.
🐯 : Recursion seems to be an intuitive approach. There are quite a few conditional checks in your code that may not be very intuitive for readability. Can you simplify those conditional checks?
🦝 : I see your point. I can calculate the middle index directly using integer division, eliminating the need for even/odd index checks.
🐯 : Good. Can you provide a brief efficiency assessment of your approach?
🦝 : In terms of time complexity, with each recursive call, the array is split into two equal halves for the next level of calls, so the time complexity is O(logN). In terms of space complexity, each value in the array is stored in a TreeNode structure, so the space complexity is O(N).
🐯 : Understood. That concludes our interview for today. Thank you for your participation.
🦝 : Thank you very much.
優點
10:14: 時間跟空間複雜度的分析簡潔又清楚,很讚。
可改進地方