--- robots: noindex, nofollow --- # positive grid 面試筆試題目 ### 筆試 #### 第一題 考 LeetCode 155 min stack 計時 10 分鐘 寫白板模擬花了 8 分鐘 約 3 分鐘把 pseudo code 寫完 然後一行一行 code review 應該沒什麼錯誤 時間複雜度 O(1) (for 每個操作) 空間複雜度 O(1) ```=c type MinStack struct { } /** initialize your data structure here. */ func Constructor() MinStack { } func (this *MinStack) Push(x int) { } func (this *MinStack) Pop() { } func (this *MinStack) Top() int { } func (this *MinStack) GetMin() int { } /** * Your MinStack object will be instantiated and called as such: * obj := Constructor(); * obj.Push(x); * obj.Pop(); * param_3 := obj.Top(); * param_4 := obj.GetMin(); */ ``` 其實看到題目當下我就直接說了這題我寫過R 是想看看對方會不會換題目,不過他們說寫過也沒差,先寫出來。 用差值的方式解完之後對方說這個方法比較 trick ,第一次看到應該不太可能想出來 我們就老實探討如果第一次看到用 O(1) O(n) 的方式解的話怎樣解 用一個陣列可以做到 O(logn) O(n),用兩個陣列模擬狀態機可以做到 O(1) O(n) #### 第二題 ```=c # include <iostream> using namespace std; class base { public: base () { cout << "base controctor" << endl; } ~base () { cout << "base detroctor" << endl; } }; class derived : public base { public: derived () { cout << "derived controctor" << endl; } ~derived (){ cout << "derived detroctor" << endl; } }; int main() { base* a = new derived(); delete a; } ``` 問輸出 實際輸出應該是 ``` base controctor derived controctor base detroctor ``` 說有沒有辦法呼叫到 `cout << "derived detroctor" << endl;` 加個 virtaul 就好 ```=c virtual ~base () { cout << "base detroctor" << endl; } ``` #### 第三題 ```=c # include <iostream> using namespace std; class base { public: virtual void print(){ cout << "derived print" <<endl; } void show(){ cout << "derived show" <<endl; } }; class derived : public base { public: void print(){ cout << "derived print" <<endl; } void show(){ cout << "derived show" <<endl; } }; int main() { base* a = new derived(); a -> print(); a -> show(); derived d; base* c = &d; c -> print(); c -> show(); return 0; } ``` 問輸出 實際答案 ``` derived print base show derived print base show ``` 順帶一提 ```=c base* a = new derived(); // and derived d; base* c = &d; ``` 兩者是等價的 口試 聊聊 vector map RAII Design Patterns dynamic_cast static_cast circular reference how to debug 後面跟 manager 還有聊到 git flow 線程模型
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up