Repeat
Example
Approach
Code
Test
Opmization
🧔 : interviewer 👶:interviewee
🧔 : 您好 今天由我來主詞這場面試,然後這邊有個問題想要請你解一下,題目敘述為
.
. / / 測驗說明與問答 如下面各題所示
.
🧔 : 方法大致沒問題 那你可以開始 implement 剛剛討論的方法了
👶 : coding. . . finished 帶入example 解釋code沒問題
🧔 : ok 那可以分析一下 你寫的程式 他的時間複雜度如何
👶 : explain time complexity 是否有更好的方法
🧔 : 好 那今天就先到這邊 稍待與主管和人資討論 再連絡你 謝謝
🧔:給予一段字串,請判斷此字串之括號是否相匹配,合法請傳回true,不合法回傳false,你可以假設所有輸入之字串僅包含,括號、中括號、大括號這三種,且字串長度至少為1,小於104。
👶:好的我可以理解為 輸入字串僅有合法括號,題目為判斷此字串之合法性,簡單帶個例子 '()[]{}' -> true , '[{]}'-> false.
那我先大致敘說目前想到的方法,我會使用stack儲存character,然後使用iterator iterate input string s 的第一個element 到最後一個element,其中每輪當遇到左括號push至stack,若非左括號則檢查stack是否為空,不為空則檢查其右括號與stack top elment是否相匹配,匹配pop出來完成該次iterate,否則回傳false.
當所有character皆檢查完後,再檢查stack是否為空,若為空則代表input為valid回傳true,反之不為空則為invalid回傳false.
測驗說明與問答
🧔: 給予一 double x and int n,請計算其冪次 x 的 n 次方,你可以假設 -100.0<x<100.0 , 而 n 於[-2^31 , 2^31-1]之間,n為非0整數,且x的n次其值落於-10的4次方與10的4次方之間.
👶: 舉個例子 2^10 = 1024, 2^-2 = 0.25.
直觀的做法為直接將x相乘n次 若n為負 則使用1/(x的n次)即可,
假設|x|: d, |n|=N; 則此方法的時間複雜度為O(N),但考量時間複雜度有更優解 故不這麼做.
可以使用 recursion 將N以2進制的方式做考慮
2^10 = 2^(0b1010) = 1* 2^2 * 2^8
2^-2 = 1/2 ^ 2 = 0.5 ^(0b10) = 0.5 ^ 2 = 0.25
但是這樣比起iterate的方式多出 遞迴函式所占用的stack空間 O(log N) 所以我想用iterator的方式
用while把 n為0 作為終止條件 若n對2取模數(module 2)不為0時 則將結果先乘起來 之後x取平方 n除等於2
🧔:Hello, welcome to the interview, there is a problem on the
google docs
The problem is to find two numbers in an integer array, 'nums,' that add up to a given 'target.' We need to return the indices of these two numbers.
👶:I think a suitable approach for this problem is to use a hash map. We can iterate through the array once, storing each element's value and its index in the hash map. Then, during the second pass, we check if the complement of the current element (target minus the current number) exists in the hash map. If it does, we've found our pair.
🧔:Do you think there's any room for optimization in your code?
👶:I believe this solution is already quite efficient. The hash map allows us to find the complement in constant time, making the overall time complexity O(n), where n is the length of the array. I don't see any further optimization needed for this problem.
🧔:Excellent! Your solution looks solid, and you've demonstrated a good understanding of the problem-solving process. Thank you for going through the REACTO framework with us. Is there anything else you'd like to add or any questions you have?
👶:No, I think we've covered everything. Thank you for this opportunity.