# TSMC & IC Design House(M,R,P)考古
## TSMC hackerrank(類似題)
- Q1 (要求O(n),不然會TLE)
leetcode 3.Longest substring without repeating characters
- Q2
leetcode 684.redundant connection
# IC Design House 面試問題
## 聯發科
## 上機程式題
### Q1
給定一個prefix(第一行輸入),輸出不包含此prefix的string
#### input:
aaaa
aaaabbbbccc
badssssd
mdjeld
bbaaaakkdkd
#### output:
badssssd
mdjeld
bbaaaakkdkd
### Solution
```cpp
void check_prefix(string input, string prefix) {
string sim_input = input.substr(0, prefix.length());
if (sim_input != prefix) {
cout << input << "\n";
return;
}
}
```
### Q2
矩陣轉置
#### input:
3 3
1 2 3
4 5 6
7 8 9
#### output:
1 4 7
2 5 8
3 6 9
### Solution
```cpp
// get input
for (int i = 0 ; i < row ; i++) {
for (int j = 0 ; j < col ; j++) {
cin >> x[i][j];
}
}
// transpose
for (int i = 0 ; i < col ; i++) {
for (int j = 0 ; j < row ; j++) {
cout << x[j][i] << " ";
}
cout << "\n";
}
```
## C語言
- Q1
:::info
(選擇題)問以下呼叫greeting 10次,print出cnt是多少?
:::
```cpp
void greeting() {
static int cnt = 0;
++cnt;
}
// solution : 10
```
- Q2
:::info
(問答題)問 j = ?
:::
```cpp
int j=0;
int n=10;
while(n--!=0)
{
j++;
}
// solution : 10
// can treat as
for (int i = n ; i != 0 ; i--) {
j++;
}
```
- Q3
:::info
(選擇題)問輸出是多少?
:::
```cpp
#define SWAP(a,b) tmp = a; a = b ; b= tmp
int a = 10;
int b = 20;
int tmp = 0;
int n = 6;
if(n > 6)
SWAP(a,b);
printf("%d%d%d",tmp,a,b);
// solution : 0, 20, 0
```
- Q4
:::info
(選擇題)哪個選項能使n = 5
:::
```cpp
int n = 3;
if(n > 2)
n = 4;
____(n > 3)
n = 5;
```
(1)else
(2)else if
(3)if
- ans : (3)
- Q5
:::info
(問答題)問輸出
:::
```cpp
int main()
{
int arr[8] = {0,1,2,3,4,5,6,7};
int* ptr1 = &arr[0];
char* ptr2 = (char*) ptr1;
printf("%d",*ptr2);
return 0;
}
```
- Q6
:::info
(多選題)問哪邊會出現錯誤(以及錯在哪),及要如何初始化?
:::
```cpp
union Var{
char ch;
int num1;
double num2;
};
int main()
{
union Var var = {123};
printf("var.ch = %c\n",var.ch);
printf("var.num1 = %d\n",var.num1);
printf("var.num2 = %.3f\n",var.num2);
return 0;
}
```
- Q7
:::info
(問答題)問輸出?
:::
```cpp
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int cnt;
for(cnt = 5 ; cnt-- ; ++a);
for(cnt = 5 ; --cnt ; b++);
for(cnt = 5 ; --cnt>0 ; ++c);
for(cnt = 5 ; cnt-->0 ; d++);
printf("%d",a*b*c*d);
return 0;
// a = 5 , b = 4 , c = 4 , d = 5
// 400
}
```
# RealTek
## 手寫考卷
- Q1
:::info
問以下pointer意思
:::
int p //⼀個整型數(An integer)
int *p //⼀個指向整型數的指標(A pointer to an integer)
int **p //⼀個指向指標的的指標,它指向的指標是指向⼀個整型數(A pointer to a pointer to an integer)
int p[10] //⼀個有10個整型數的陣列(An array of 10 integers)
int *p[10] //⼀個有10個指標的陣列,該指標是指向⼀個整型數的(An array of 10 pointers to integers)
int (*p)[10] //⼀個指向有10個整型數陣列的指標(A pointer to an array of 10 integers)
int (*p)(int) //⼀個指向函數的指標,該函數有⼀個整型參數並返回⼀個整型數(A pointer to a function that takes an integer as an argument and
int (*p[10])(int) //⼀個有10個指標的陣列,該指標指向⼀個函數,該函數有⼀個整型參數並返回⼀個整型數
- Q2
:::info
inline的好處
:::
inline 可以將修飾的函式設為⾏內函式,即像巨集 (define) ⼀樣將該函式展開編譯,⽤來加速執⾏速度。
- Q3
:::info
global,local,static 變數的生命週期
:::
local:只存在當下函式執行期間
static:變數宣告時若加上 static,執行時期會一直存在記憶體的固定位置
global:全域變數的生命週期始於程式開始,終止於程式結束
- Q4
:::info
解釋const
:::
被const所修飾到的內容是不可變的。
- Q5
:::info
reverse string in C?
:::
```cpp
#include <stdio.h>
#include <string.h>
void reverse(char*, int, int);
int main()
{
char a[100];
gets(a);
reverse(a, 0, strlen(a)-1);
printf("%s\n", a);
return 0;
}
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}
```
- Q6
:::info
call by value, call by reference?
:::
https://wayne265265.pixnet.net/blog/post/112556555-%E3%80%90%E6%95%99%E5%AD%B8%E3%80%91call-by-value%2C-call-by-address%2C-call-by-referenc
- Q7
:::info
Explain "volatile" ?
:::
volatile 修飾的變數代表它可能會被不預期的更新。volatile 是在指⽰編譯器每次對該變數進⾏存取時都要「⽴即更新」,不應該
對其做任何最佳化。不存取register的值,⽽是直接從記憶體取值。
- Q8
:::info
#define MIN(A,B) A < B ? A:B; 這樣會有甚麼問題?
:::
```cpp
int result = 2 * MIN(6,10); // 2*6<10?6:10;
//result 會變成10,但我們要的是12
```
# 群聯
## 手寫考卷
- Q1
:::info
- Write a fuction clear the 18th bit of an unsigned integer
- setbit
- reverse bit
:::
```cpp
void clearBit(unsigned int& x){
x = x & ~(1 << 17);
}
void setbit(unsigned int& x){
x = x | (1 << 17);
}
void reverseBit(unsigned int& x){
x = x ^ (1 << 17);
}
```
- Q2
:::info
reverse string
:::
同上
- Q3
:::info
給一個struct,問你這個struct佔幾byte?
:::
https://www.itread01.com/content/1549653142.html
- Q4
:::info
已排序好的arr, Binary Seach 找 key,有的話回傳true,沒回傳的話false
:::
```cpp
bool binarySearch(vector<int>& nums, int target,int start ,int end)
{
if(start<=end)
{
int mid = (start + end) / 2;
if(nums[mid] == target)
return true;
else if(nums[mid] < target)
return binarySearch(nums, target, mid+1, end);
else
return binarySearch(nums, target, start, mid-1);
}
return false;
}
```
- Q5
:::info
Linked list 實作queue的各種操作,push, pop, getfront, getback, isempty, qsize
:::
- Q6
:::info
問輸出
:::
```cpp
int main()
{
unsigned int a = 10;
for(a; a>=0; --a){
cout<<a<<endl;
}
return 0;
}
```
10
9
.
.
.
0
4294967295
4294967294
.
.
.
1
0
4294967295
重複下去
## 白板題
:::info
0~500個數字每次隨機 取一個數字出來,但下次在抽出時不可以出現已經抽過的數字,問你如何時實現。
:::
https://leetcode.com/problems/shuffle-an-array/
```cpp
pseudo code
vector<int>v(500,0);
for(int i = 1 ; i <= 500 ; i++)
v[i] = i;
for(int i = 499 ; i >= 0 ; i--)
{
int randIndex = rand(0,i);
cout<<v[randIndex]<<" ";
swap(v[randIndex],v[i]);
}
// Time complexity O(n)
```