「真希望 Google 能成功的收到更多台灣人。進去的條件就像聯考。不知為何,明明台灣人考試不弱,但會這種制式面試的卻相對少… 面試以外的能力固然重要,可是你的競爭者就是除了面試比你強外,其他也很強啊。光是母語不同、開發專案的知名度不同、待過的學校不同等,就狠耍一條街了。相較之下面試題真的是最容易準備的了」 – from a Googler
video: How to Get a Job at the Big 4 - Amazon, Facebook, Google & Microsoft
貢獻者: 林克-Link
時間軸: 00:02:08-00:26:47
Given a sorted array arr of distinct integers, write a function indexEqualsValueSearch
that returns the lowest index i
for which arr[i] == i
. Return -1
if there is no such index. Analyze the time and space complexities of your solution and explain its correctness.
Examples:
Constraints:
Since input is at most contain 100 element. Use for loop to iterate all index. If find the element equal to index return index. Otherwise return -1
.
Time complexity: 疊代所有 index,直到 arr[i] == i
或 arr 中最後一個元素,複雜度為 。
Space complexity: 沒有使用額外的空間,複雜度為 。
因為 arr 已排序過,所以我們可使用 binary search 這類策略或技巧,一開始宣告兩個變數 lo
和 hi
,用來記錄搜索範圍。每輪搜索中確認 arr 中索引值為 mid 的 element , 分成兩個狀況:
mid <= arr[mid]
代表 feasible region 小於等於 mid
,將搜索區間的上界設為 mid
mid
,下界調整為 mid + 1
結束 while 迴圈時 lo == hi
,若此時答案不存在 arr[lo] != lo
,回傳 -1
。
Time complexity: 起出搜索空間為 0 到 N - 1
, 每一輪都將搜索空間縮小一半,因此經過 lgN 輪後便能找到 the lowest index,時間複雜度為 O(logN)
。
Space complexity: 僅宣告 lo
, hi
, mid
等變數,複雜度為 。
logN
僅粗略帶過。from TechZoo
時間軸: 00:26:47-1:14:36
The awards committee of your alma mater (i.e. your college/university) asked for your assistance with a budget allocation problem they're facing. Originally, the committee planned to give N research grants this year. However, due to spending cutbacks, the budget was reduced to newBudget dollars and now they need to reallocate the grants. The committee made a decision that they’d like to impact as few grant recipients as possible by applying a maximum cap on all grants. Every grant initially planned to be higher than cap will now be exactly cap dollars. Grants less or equal to cap, obviously, won’t be impacted.
Given an array grantsArray of the original grants and the reduced budget newBudget, write a function findGrantsCap that finds in the most efficient manner a cap such that the least number of recipients is impacted and that the new budget constraint is met (i.e. sum of the N reallocated grants equals to newBudget).
Analyze the time and space complexities of your solution.
Example:
Constraints:
Time complexity: 算出 arr 的合需要 ,while 迴圈中每輪會花 的時間執行,需要執行 其中 P 為 double 的 precision, 因爲搜索空間為浮點數,而非整數。
Space complexity: 僅有一些變數宣告,因此空間複雜度為常數,也就是 O(1)。
Time complexity:
Space complexity:
2021/12/30