# 準備心得
1. 先複習作業系統、計算機結構基本知識,以及C或C++的語法,要能夠回答出各種「請解釋OOO是什麼」、「請問OOO和XXX的差別是什麼」。
2. 承1,試著問自己「為什麼要設計(使用/發明)OOO」、「XXX有什麼優缺點」、「假如不用OOO,那要怎麼實作」之類的問題,少數面試官會follow up問到這層,我認為是以前在學校時不會想過的問題,比較需要事前準備。
3. 準備的過程中可以用AI幫忙,我的作法是會將我的答案寫出來,問AI有沒有錯誤或是需要補充的地方。[Perplexity](https://www.perplexity.ai/)和[Grok](https://grok.com/)都有每日免費deep search之類的次數可以使用。
# 作業系統、計算機結構
* Process和Thread
* Semaphore, Mutex, Spinlock
* Mutex跟binary semaphore有差別嗎?
* Cache, MMU, TLB
* Virtual Address如何轉成Physical Address
* What's advantage of virtual memory?
* Race condition
* 舉例Interprocess communication
* Deadlock是什麼?Deadlock的發生條件?怎麼避免?
* Compiler和Linker各自做哪些事情?
* Static linking和dynamic linking的差別?
* 從assembly的角度說明function call和interrupt發生時會有哪些行為
# C++
* What is OOP?
* 為什麼要設計OO?
* 什麼是RAII?舉例哪些地方有用到RAII?什麼情況下不適合使用RAII?
* Pass by value和pass by reference的差別?
* Pass pointer to function是pass by value還是pass by reference?
* 用`delete`去釋放`malloc`出來的東西,以及用`free`去釋放`new`出來的東西,會發生什麼事情?
* `const`放在class method parameter list後面,`const`修飾的對象是什麼?
```cpp!=
class bar {
public:
void func() const {
// do something
}
};
```
# 軟韌C考題
1. 什麼是`static`, `extern`, `volatile`, `inline`, `union`?
2. 熟不熟operator precedence?Prefix/Postfix `++`/`--` 和dereference一起出現的時候,會發生什麼事情?
```c!=
#include <stdio.h>
int main() {
char s[] = "0123456";
char *p = s;
printf("%c\n",*p++); // ?
printf("%c\n",*(p++)); // ?
printf("%c\n",(*p)++); // ?
printf("%c\n",*++p); // ?
printf("%c\n",*(++p)); // ?
printf("%c\n",++*p); // ?
printf("%c\n",++(*p)); // ?
printf("%s\n",s); // ???????
printf("p at [%d]\n", (int)(p - s)); // p at [?]
return 0;
}
```
3. 寫一個程式判斷一個系統是little endian還是big endian
4. 熟不熟`union`和`struct`?什麼是`#pragma pack()`?`struct`內同時有不同大小的變數,不同宣告的順序下,一個`struct`的大小會有什麼變化?
```c!=
#pragma pack(1)
typedef struct A_ {
int a;
char c;
double d;
}pack_1;
#pragma pack(2)
typedef struct B_ {
int a;
char c;
double d;
}pack_2;
#pragma pack(4)
typedef struct C_ {
int a;
char c;
double d;
}pack_4;
#pragma pack(8)
typedef struct D_ {
int a;
char c1;
char c2;
double d;
}pack_8_1;
#pragma pack(8)
typedef struct E_ {
char c1;
double d;
int a;
char c2;
}pack_8_2;
int main() {
printf("%lu\n", sizeof(pack_1)); // ???
printf("%lu\n", sizeof(pack_2)); // ???
printf("%lu\n", sizeof(pack_4)); // ???
printf("%lu\n", sizeof(pack_8_1)); // ???
printf("%lu\n", sizeof(pack_8_2)); // ???
return 0;
}
```
```c!=
#include <stdio.h>
struct A {
unsigned short a1;
unsigned short a2;
unsigned short a3;
};
union B {
unsigned long long b1;
struct A b2;
};
int main() {
union B uu;
printf("%I64u\n", sizeof(uu)); // 8 bytes due to long long
uu.b1 = 0xffffffffffffffff;
uu.b2.a1 = 0x1234;
uu.b2.a2 = 0x5678;
uu.b2.a3 = 0xabcd;
printf("%I64x\n", uu.b1); // ?
*(&(uu.b2.a3) + 1) = 0xeeee;
printf("%I64x\n", uu.b1); // ?
return 0;
}
```
5. Array換個type來讀,會發生什麼事情?
```c!=
short arr[] = {0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666};
int a = ((int*)arr + 1)[1]; // a = 0x?
int b = ((int*)(arr + 2))[1]; // b = 0x?
int d = ((int*)(arr + 1))[1]; // d = 0x?
int c = *(int*)(arr + 3); // c = 0x?
int e = (arr + 1)[1]; // e = 0x?
int f = *(short*)((int*)arr + 1); // f = 0x?
int g = ((short*)((int*)arr + 1))[0]; // g = 0x?
int h = ((short*)((int*)arr + 1))[1]; // h = 0x?
```
6. Bitwise操作。怎麼算一個數值的binary有幾個1?交換變數值?字母大小寫轉換?
7. 用中英文說明複雜的declaration
```c!=
int **p; // 1. p是...
int a[10]; // 2. a是...
int *p[10]; // 3. p是...
int (*p)[10]; // 4. p是...
int (*p)(int); // 5. p是...
int (*p[10])(int); // 6. p是...
```
8. 熟不熟`const`修飾?哪些相等?哪些pointer本身是`const`?
```c!=
const int * a;
int const * b;
int * const c;
int const * const d;
```
9. 各種Singly linked list的操作,insert at head/tail, remove node, reverse linked list, merge two sorted list, find middle element of list。假想自己被問到這些問題,有沒有辦法直接寫出code?有沒有辦法處理corner caes?
10. MACRO
```c!=
#include <stdio.h>
#define GET_MAX(a, b, mask) (a&mask) > (b&mask) ? (a&mask) : (b&mask)
int main() {
unsigned short i = 12;
unsigned short j = 9;
printf("%d\n", GET_MAX(i++,++j, 0x7)); // ?
printf("i=%d j=%d\n", i, j); // ?
return 0;
}
```
11. Ternary Operator `?:`
Short-circuit evaluation,`A && B`假如A已經是`false`就不會evaluate B;`condition ? exprA : exprB`,假如`condition`是`true`就不會evaluate`exprB`。
```cpp!=
unsigned short i = 12;
unsigned short j = 9;
#define GET_MAX(a,b) a > b ? a : b
printf("%d\n", GET_MAX(i++,++j)); // ??
printf("i=%d j=%d\n", i, j); // i=?? j=??
```
12. 找質數寫出來並分析時間、空間複雜度。
13. Fibonacci:用Loop和Recursion實作;怎麼不用loop讓Fibonacci數列無窮的印下去?