# 2026 面試 - ASML SWE ## Job Description :::spoiler D&E Field – Taiwan Factory Engineering – Software Engineer - Linkou > - Role > D&E Field – Taiwan Factory Engineering – Software Engineer - Linkou > - Introduction to the job > - We are looking for experienced software engineers who can support the manufacturing process of DUV modules at Linkou to meet the output and quality requirements for these DUV modules. > - The vision is to achieve a more scalable, competitive and closer-to-the customer manufacturing capability in Taiwan. This job is a new strategic opportunity to contribute to this vision and at the same time gives you the opportunity to learn more about how ASML operates. > - You will work in a new team of approximately 40 fte (projected to grow further) of multiple competences. You will work closely together with the manufacturing team (~70 fte) and Supplier Quality Engineers (~10 fte). This team is part of the D&E Field organization with an overarching mission to increase intimacy with our customer and ASML operations (Customer Support, Repair & Re-use, Manufacturing and Strategic Sourcing and Procurement). > - This position is open to internal local hires as well as Long Term assignees (LTA) within ASML. If you are interested in an LTA-position, please refer to the LTA job description for Software Engineer in workday. > - Depending on your background, a period of training (26Q2-Q3 timeframe) might be necessary in Veldhoven or Wilton, USA. > > - Roles and responsibilities > To build the manufacturing capability at New Taipei Campus we need software engineers who will: > - Support the DUV module manufacturing organization with high end SW maintenance and troubleshooting/diagnostics capability to keep all tooling functional. > - Ensure continuity of production by resolving SW related issues in production. Creating small containment patches could be necessary whereas normal/more complex issues need to be resolved in conjunction with central D&E in Veldhoven. > - Resolves module SW-related AIR issues in terms of CT, diagnosis, and quality improvement. > - Support the transfer of suppliers by resolving the SW related issues. > - Communicate and synchronize with engineers & architects from other disciplines (electrical, mechanical, mechatronics, metro, optical). > > - Education and experience > - You have preferably D&E level SW knowledge in one or more of the following DUV modules: Reticle Stage, Wafer stage, Metro, Illumination including Lens assy. Also, you have the hands-on knowledge on how to make SW change within ASML, SW development WoW, and follow the official review process while working in close alignment with central D&E. You have Affinity/knowledge of standard ASML diagnostic systems (for example metro-analyzer etc.) and master at least one program language (C++, Python, C). You have an inherent motivation for hands-on technical support and the ability to fail fast and learn fast. > - and ≥ 4 years of experience in a software engineer role or relatable operational experience within ASML. > > - Skills > To thrive in this job, you will need the following skills: > - Being a generalist in your field of expertise, willing to dive into unknown problems and the ability to operate in a multi-disciplinary team. > - Is familiar with the manufacturing way of working. > - Understands the high level functional and engineering principles of various parts and modules of manufactured systems. Being creative, inventive, and thinking out-of-the box to realize solutions in an ambiguous environment where not all design documentation is up-to-speed. > - A pioneering mindset, self-propelling, a pragmatic approach, and showing initiative; > - Enthusiastic and ambitious professional with ability to communicate clearly and concisely to peers and customers combined with stakeholder management skills to operate across sites and sectors. > - A quick learner, in for a challenge, continuously drive for improvement, and enjoy achieving results together with your team. > - Working level proficiency in English language. > > - Diversity and inclusion > ASML is an Equal Opportunity Employer that values and respects the importance of a diverse and inclusive workforce. It is the policy of the company to recruit, hire, train and promote per-sons in all job titles without regard to race, color, religion, sex, age, national origin, veteran status, disability, sexual orientation, or gender identity. We recognize that diversity and inclusion is a driving force in the success of our company. > > - Other information > - This position may require access to controlled technology, as defined in the Export Administration Regulations (15 C.F.R. § 730, et seq.). Qualified candidates must be legally authorized to access such controlled technology prior to beginning work. Business demands may require the Company to proceed with candidates who are immediately eligible to access controlled technology. > - This position requires access to controlled technology, as defined in the United States Export Administration Regulations (15 C.F.R. § 730, et seq.). Qualified candidates must be legally authorized to access such controlled technology prior to beginning work. Business demands may require ASML to proceed with candidates who are immediately eligible to access controlled technology. ::: ## Progress | Date | Progress | |---|---| | D+0 (2026/1/4) | Submitted Application | | D+3 | Virtual interview Invitation | | D+5 | Virtual interview with HR (Dean Wu) | | D+12 | 1st round interview with TW Team (90 min). 25 mins Self-introduction (All English), 35 mins QA (All English), 30 mins coding test (C language). (HR Dean Wu's advice on the self-intro: technical related, skills, competency, within 6 page of ppt deck is enough, big picture don't deep dive.)| | D+26 | Rejected | ## Interview Details 面試官有兩位,Jacky Lai 和 Louis Hsieh,前者是台灣 D&E Group Lead,後者為 Software Architect。基本上面試官就照著我的 slide 不時打斷問問題,直到自我介紹結束,結束後馬上接 C coding test。 {%pdf https://drive.google.com/file/d/16OFLuITujqExM8nMXTkAiZWyKAhVa5oC/view?usp=sharing %} 他們寫了一個網頁 IDE,左側可寫 code,右側顯示 ChatGPT 和 log,考的題目只有一題,為:有個 encoded_msg 在 main() 裡,請完成一個 decode_msg() 函式可以對 encoded_msg reverse byte 並對每個 byte 扣去 0x64。 完整程式按照印象大概如下: ```c= #include <stdio.h> /** * Decodes the message by subtracting 0x64 from each byte * and reversing the array order. */ void decode_msg(unsigned char arr[], int size) { // Step 1: Subtract 0x64 from each element for (int i = 0; i < size; i++) { arr[i] = arr[i] - 0x64; } // Step 2: Reverse the array using a swap logic int start = 0; int end = size - 1; while (start < end) { unsigned char temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } int main() { // Example encoded message (hex values) // Let's assume the hidden message is "Hello" reversed and shifted unsigned char encoded_msg[] = {0xAC, 0xD0, 0xD0, 0x69, 0xAC}; int size = sizeof(encoded_msg) / sizeof(encoded_msg[0]); printf("Original hex: "); for(int i = 0; i < size; i++) printf("0x%02X ", encoded_msg[i]); // Decode the message decode_msg(encoded_msg, size); printf("\nDecoded string: "); for (int i = 0; i < size; i++) { printf("%c", encoded_msg[i]); // Print as characters } printf("\n"); return 0; } ``` 我答得很爛,但是過程中有不斷和面試官保持對話,思路也有 murmur 說出來讓他知道。最後寫出來一個應該是有成功反轉的 decode_msg,但是還是有 COMPILER ERROR,這時 30 分鐘就到了,只能停止作答。 ## QAs (He questioned; I answered) >[!Warning] His Question > 我聽你前面報告說你熟悉 memory management,但是這麼簡單的問題怎麼要你寫就有問題,正常來說寫過 C/C++ 都會擅長啊? >[!Note] My Answer (我有苦說不出。)因為機器人寫 ROS2 基本上也只是在 call 別人包好的 API 或函式,沒有實作底層這些功能。另外我大部分時間都在寫模擬用的 Python script,用一個星期惡補這些底層 C 開發能力實在是強人所難。但是我有跟他說明我寫的機器人控制器偏上方 ROS2 應用層居多,偶爾才看得到同仁寫的 C code,因此雖然 C code 開發能力弱但是能看得懂。 >[!Warning] His Question > 那你在學校修過什麼軟體課? >[!Note] My Answer > 我修過兩門程式語言: C 語言跟 Python、資料結構、演算法(但沒有說是在 Udemy 自學的),跟旁聽作業系統(我也沒說我是旁聽清大 OCW 周志遠教授的作業系統),但是對於軟體工程這些就比較沒有碰到,因此對 Design Pattern 就不太熟。(面試官補了一句說至少你還知道說得出這些詞。) >[!Warning] His Question > 基於你的背景,你應該是拿軟體當作工具而已,你真的有辦法做這些底層開發嗎或是就算你可以進來你會有興趣嗎?(後續他說我的背景應該是偏上層、做研究,拿軟體當作工具而已,對軟體工程應該是要補蠻多東西的。話裡話外聽得出來他滿輕視上層研究的,因為他說底層開發需要你實作去學習,比如說剛剛的題目如果有寫過一個 APP 應該都能打得出來,但是對於演算法,我查個 paper 應該都能知道答案。) >[!Note] My Answer > 我又繼續有苦說不出了,我仔細跟他說我其實是想要往下開發底層的,因為上層研究基本上在台達都被切給研究院(DRC、DRIC)負責了,而下層基本上是 BGBU 才會負責開發產品。我的這個特別部門基本上被夾在中間,做一些系統整合幫助研究院的成果在工廠裡作加值型的功能或服務。但是有開始碰到控制器團隊,所以有找到一個切入點,開始了解單純的軟體開發。 >[!Caution] 算法崗 vs. 開發崗 > 我這個問題基本上就是算法崗跟開發崗的抉擇——捲算法,我又沒機器人、AI 背景,甚麼屁算法也不懂,就算做計算力學算法,業界也沒有需求。此外我競賽、論文、頂會文章十項全無;捲開發,目前針對產品(網站、APP,有硬體的實體產品)做完整的開發的公司或單位就面不進去,只能陷入要有開發經驗才能找開發工作,但沒做開發工作就沒開發經驗的死亡迴圈。這樣能理解一畢業就找到兩者其一的含金量了嗎?我真他媽 > - [算法岗还是开发岗, LeetCode](https://leetcode.cn/discuss/post/3145590/suan-fa-gang-huan-shi-kai-fa-gang-by-luo-gu3q/) > - [算法岗和开发岗该怎么选择?, CSDN](https://blog.csdn.net/woshicver/article/details/132033517) > - [知乎精选 | 长远看算法岗真的比开发岗香吗?](https://cloud.tencent.com/developer/article/2230378) >[!Warning] His Question > 他問我說你不會覺得要繼續做研究嗎?因為往下走就碰不到 AI? >[!Note] My Answer > 我說我覺得科技巨頭才有能力開發模型,其他的公司只能拿來應用,而我覺得對 AI 應用沒有興趣,我想往下走打穩嵌入式能力,即使想走 AI 還有邊緣運算可以走。(就說了 DRC、DRIC 才會負責研究,我們單位就做不到研究啊,那我之前研究經歷又有什麼用?所以我才想轉開發崗啊,那我們單位也做不到開發啊,廢話當然沒開發經驗,鬼打牆。) ## QAs (I questioned; he answered) >[!Warning] My Question > 部門的配置跟分工怎麼樣呢? >[!Note] His Answer > 基本上 D&E 裡面分了四個部門,分別對應四種機台。每個部門裡面也有分成機構、控制、硬體、軟體。 >[!Warning] My Question > 我覺得像這次的面試也是一個很寶貴的經驗,那請問你對我有甚麼建議嗎? >[!Note] His Answer > 要有產品的思維,因為技術不值錢,產品才值錢。 >[!Warning] My Question > 那我現在的背景一個新鮮人也碰不到需要高階思維的角色,基本上也是在開發者的層級工作,那我面臨到技術能力不足開發不出好產品怎麼辦? >[!Note] His Answer > ...。(他也不知道如何回答) >[!Warning] My Question > 這個職位是協助荷蘭的演算法工程師把演算法放到機台內嗎? >[!Note] His Answer > 不是,這個職位是純粹的軟體工程,目前碰到的專案的關係才會做到一些 JD 寫的事項,但通常就是 general SDE。 >[!Warning] My Question > 為甚麼是寫 C,不是用 C++ 或 C#? >[!Note] His Answer > 他說對正常給工廠都會用 C# 嘛,畢竟給 Windows 都會用 C# 開發,但是目前碰到的專案是為了幫海外生產的機台搬到台灣生產,拿到一大包 legacy code,才需要寫到 C。 他在後面對我的提問回答時又不時穿插說道不知道我會不會對純粹的軟體工程有興趣。我都一一回答說我有多願意學習跟會如何補足這塊知識,希望他會看到我的 Learning Potential。最後他說會跟老闆討論看看,就結束面試。 ## Pre-Test Preparation 以下是準備 Coding Test 的練習,先前有和 HR Dean 確認考的內容,他說: > Coding test不是考 debug 也不是考演算法,就是單純完成某個(很)小功能,只是要測試 candidate 有足夠的 programming 經驗,可以完成一個完整的程式。如果有確實使用 C/C++ 完成過幾個 project 的話,應該都會覺得很簡單。 因此練習的方向大概為指針、記憶體等 C、C++ 特有的特性,不過避免漏掉知識點,還是有儘量涵蓋大部分方向。 ### 預計使用的 Vibe Coding 流程 Vibe coding Workflow 1. Theory/Logic (AI) 2. Possible Edge Cases (AI) 3. Implementation (HUMAN) 4. Code Auditor (AI) 5. Refinement (HUMAN) ### Macro > 用預處理指令 #define 聲明一個常數,用以表明1年中有多少秒(忽略閏年問題) ```c= #define SECS_PER_YEAR (86400*365UL) ``` > 寫一個"標準"巨集MIN ,這個巨集輸入兩個參數並返回較小的一個。 ```c= #define MIN(A, B) ((A)<=(B)? (A):(B)) ``` > 寫一個define可以回傳陣列長度 ```c= #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0])) ``` ### Hardware-Aware Optimization > 保證 n 一定是上面五個數字之一 不能用if 和 switch case , 請用你認為最快的方法實作 main ```c= extern void func1(void); extern void func2(void); extern void func3(void); extern void func4(void); extern void func5(void); void main(int n) { if n==1 execute func1; if n==2 execute func2; if n==3 execute func3; if n==4 execute func4; if n==5 execute func5; } ``` ```c= // 使用 「函式指標陣列 (Array of Function Pointers)」。 // 這在底層開發中被稱為 Jump Table (跳躍表)。 // if-else 或 switch「分支預測」,會消耗 CPU 週期。 // 直接透過索引值(Index)找到記憶體位址並跳轉,時間複雜度是 $O(1)$ void main(int n) { void (*func_table[]) (void){ func1, // index 0 func2, // index 1 func3, func4, func5 }; func_table[n - 1](); } ``` > 保證 n 一定是上面五個數字之一,使用if, 請用你認為最快的方法實作main ```c= extern void func1(void); extern void func2(void); extern void func3(void); extern void func4(void); extern void func5(void); void main(int n) { if n==33 execute func1; if n==67 execute func2; if n==324 execute func3; if n==231 execute func4; if n==687 execute func5; } ``` ```c= #include<stdio.h> extern void func1(void); extern void func2(void); extern void func3(void); extern void func4(void); extern void func5(void); void main(int n) { if (n == 231) { func4(); } else if (n < 231){ if (n == 67){ func2(); } else func1(); }else{ if (n == 324) func3(); else func5(); }; } ``` ### Basic Logic > 輸入3個數, 找出最大和最小值 ```c= // value version #include <stdio.h> // 1. Define the structure first struct ret_val { int max; int min; }; // 2. The function logic you chose struct ret_val find(int a, int b, int c) { struct ret_val ret; // Initialize both to the first value ret.max = a; ret.min = a; // Compare with b if (b > ret.max) ret.max = b; if (b < ret.min) ret.min = b; // Compare with c if (c > ret.max) ret.max = c; if (c < ret.min) ret.min = c; return ret; } // 3. The main function to execute the code int main() { int x = 15, y = 42, z = 7; // We create a struct variable to hold the returned result struct ret_val result = find(x, y, z); // Access the members using the dot (.) operator printf("Input numbers: %d, %d, %d\n", x, y, z); printf("Maximum: %d\n", result.max); printf("Minimum: %d\n", result.min); return 0; } ``` ```c= //ptr version #include <stdio.h> struct ret_val { int max; int min; }; // Use pointers (*) to modify the struct directly in memory void find(int a, int b, int c, struct ret_val *res) { if (res == NULL) return; // Safety check! res->max = a; res->min = a; // Check b if (b > res->max) res->max = b; if (b < res->min) res->min = b; // Check c if (c > res->max) res->max = c; if (c < res->min) res->min = c; } int main() { struct ret_val my_result; // Pass the address of the struct using '&' find(10, 20, 5, &my_result); printf("Max: %d, Min: %d", my_result.max, my_result.min); return 0; } ``` > 大小寫轉換 ```c= // 大寫'A'在ascii是65 // 小寫'a'在ascii是97 // 兩個差32 char convert(char c) { if (c >= 'A' && c <= 'Z') return c + 32; else if (c >= 'a' && c <= 'z') return c - 32; else return 0; } ``` > Write a program to print fibonacci number ```c= /* recursive */ int fib_recursive(int n) { if (n <= 1) return n; return fib_recursive(n-2) + fib_recursive(n-1); } /* DP */ int fib_dp(int n) { int dp[1001] = {0}; dp[0] = 0; dp[1] = 1; if (n <= 1) return dp[0]; if (n == 2) return dp[1]; for (int i = 2; i < n; i++) { dp[i] = dp[i-1] + dp[i-2]; } return dp[n-1] + dp[n-2]; } ``` > Write a swap function ```c= void swap(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } ``` > Print stars as ```text * ** *** **** ``` ```c= #include<stdio.h> #include<stdlib.h> int main(){ for (int i = 0; i < 4; ++i){ for (int j = 0; j <= i; ++j){ printf("*"); } printf("\n"); } getchar(); return 0; } ``` > Calculate the GCD (最大公因數), LCM (最小公倍數) ```c= #include<stdio.h> #include<stdlib.h> // /* Soln1, recursive, Notice: b must > a */ // int gcd_recursive(int a, int b) // { // if (b == 0) return a; // return gcd_recursive(b, a%b); // } /* Soln2, loop */ int gcd(int a, int b) { while(a != b) { if (a > b) a -= b; if (b > a) b -= a; } return a; } /* As for Least Common Multiple */ int lcm(int a, int b) { return a * b / gcd(a, b); } int main(){ int a = 12, b = 18; // printf("Enter two numbers: "); // scanf("%d %d", &a, &b); printf("GCD is %d\n", gcd(a, b)); printf("LCM is %d\n", lcm(a, b)); getchar(); // To pause the program return 0; } ``` > Find 1 ~ 100 prime ```c= #include<stdio.h> #include<stdlib.h> /* Soln2, loop */ void find_prime(int n){ for (int i = 2; i < n; ++i){ int flag = 1; for (int j = 2; j < i; ++j){ if (i%j == 0){ flag = 0; break; }; } if (flag) printf("find prime num %d\n", i); } } int main(){ // int a = 540, b = 840; find_prime(100); getchar(); // To pause the program return 0; } ``` > Write a program can round off ```c= #include<stdio.h> #include<stdlib.h> int roundoff(float num){ return (int) (num += 0.5); } int main(){ // int a = 540, b = 840; int ans = roundoff(2.7); printf("%d", ans); getchar(); // To pause the program return 0; } ``` > Write a code that check the input is a multiple of 3 or not without using division or mod. ```c= int isMultipleOf3(int n){ if (n < 0) n=-n; if (n == 0 || n == 3) return 1; if (n == 1 || n ==2) return 0; while (n > 3) { n = (n >> 2) + (n & 3); // n>>2 == n/4 左邊剩下來的位元 // n & 3(0011) == n%4 最右邊的2個位元 } // while (n > 5) { // n = (n >> 4) + (n & 15); // n/16 + n%16, 找被5整除 // } return (n == 3 || n == 0)? 1:0; } int main (){ int testNumbers[] = {0, 3, 100}; int size = sizeof(testNumbers) / sizeof(testNumbers[0]); // 元素數量 = arr的byte/單個int的byte for (int i = 0; i < size; ++i){ int num = testNumbers[i]; int result = isMultipleOf3(num); printf("%4d: %s\n", num, result); // %4d 不等於3digit+'\0', '\0' 只跟string有關 } } ``` > 寫一個 function 可傳入正整數參數 N,回傳 1 + 2 + 3 +...+N的和 ```c= #include<stdio.h> long long sumToN(int num){ if (num <= 0) return 0; long long ans = (long long)num * (num + 1)/2; return ans; } int main() { int n = 65535; printf("%lld\n", sumToN(n)); getchar(); return 0; } ``` > 寫出一個function判斷輸入的數是2的次方 ```c= #include<stdio.h> static inline int isPowerOfTwo(int num){ return (num != 0) && (!(num&num-1)); } int main(){ int test = 8; printf("%d\n", isPowerOfTwo(test)); getchar(); return 0; } ``` ### Pointer / Memory > 設置一絕對地址為0x67a9的整型變量的值為0xaa66。編譯器是一個純粹的ANSI編譯器。 ```c= int *ptr; ptr = (int *)0x67a9; // 要記得Typecast, 0x67a9 是一個 Integer Literal *ptr = 0xaa66; ``` > Write two functions that are able to access a fixed memory location ```c= /* 64-bits machine */ int read_mem(unsigned int addr) // address 通常用 unsigned int { int *p = (int *)addr; return *p; } void write_mem(unsigned int addr, int data) { int *p = (int *)addr; *p = data; } ``` > 給一段C的程式片段, 問變數配置在哪些地方 ```c= int global_var = 0; //global初始化區(data) int *global_ptr; //global未初始化區(bss) int main() { int local_var; //stack char str[] = "1234"; //stack char *p1; //stack static int static_var = 0; //global初始化區(data) p1 = (char *)malloc(sizeof(char *)); //heap free(p1); return 0; } ``` ### Linked List > Write a function to find the middle field of singled-linked list without traverse whole list. ```c= // 因為快指標的速度剛好是慢指標的 2 倍,所以:當快指標到達終點(長度 $N$)時, // 慢指標剛好會跑完一半的距離($N/2$)。Floyd's Tortoise and Hare Algorithm, // ref: Linked List Cycle I, Linked List Cycle II. #include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node* next; } Node; int getMiddleEle(Node* head){ if (head == NULL) return -1; Node *slow = head; Node *fast = head; while (fast != NULL && fast->next != NULL){ fast = fast->next->next; slow = slow->next; } return slow->data; } // 輔助函式:建立新節點 Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) return NULL; // 記憶體配置失敗檢查 newNode->data = data; newNode->next = NULL; return newNode; } int main() { // 1. 建立一個 1 -> 2 -> 3 -> 4 -> 5 的鏈結串列 Node* head = createNode(1); head->next = createNode(2); head->next->next = createNode(3); head->next->next->next = createNode(4); head->next->next->next->next = createNode(5); // 2. 測試功能 int middle = getMiddleEle(head); printf("The middle element is: %d\n", middle); // 3. 釋放記憶體 Node* temp; while (head != NULL) { temp = head; head = head->next; free(temp); } return 0; } ``` > [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/) > Given the head of a singly linked list, reverse the list, and return the reversed list. > Input: `head = [1,2,3,4,5]` ```c= /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode* head) { if (head == NULL || head->next == NULL) { return head; } struct ListNode *prev = NULL; struct ListNode *curr = head; struct ListNode *next = NULL; while (curr != NULL) { // A. 先存下「下一個」節點,否則斷開後會找不到它 next = curr->next; // B. 反轉:將目前的節點指向「前一個」 curr->next = prev; // C. 移動:將 prev 和 curr 都往後移一步 prev = curr; curr = next; } // 當 curr 變為 NULL 時,prev 剛好指向新的頭部 return prev; } ``` ### String > [Reverse String](https://leetcode.com/problems/reverse-string/description/) ```c= // two ptrs #include<stdio.h> #include<string.h> void reverseString(char* s, int sSize) { if (s == NULL || sSize < 2) return; int left = 0; int right = sSize - 1; char temp; while (left < right) { temp = s[left]; s[left] = s[right]; s[right] = temp; left++; right--; } } int main(){ char test[] = "thisisastring"; reverseString(test, strlen(test)); printf("%s\n", test); getchar(); return 0; } ``` ```c= // using swap void swap(char *c1, char *c2) { char tmp = ' '; tmp = *c1; *c1 = *c2; *c2 = tmp; } char *reverse_str(char *str) { const int len = strlen(str); for (int i = 0; i < len/2; i++) swap(&str[i], &str[len-1-i]); return str; } ``` > strcpy vs memcpy ```c= #include<stdio.h> #include<string.h> int main(){ const char *str1 = "abc\0def"; char str2{16} = {0}; char str3{16} = {0}; strcpy(str2, str1); memcpy(str3, str1, sizeof(str3)); // sizeof(str3) == 8 printf("str2 = %s\n", str2); // str2 = abc printf("str3 = %c\n", str3[5]); // str3 = e } ``` > Complete strcpy, strcat, strlen, strcmp ```c= /* strcpy */ void strcpy(char *dest, const char *src) { if (dest == NULL || src == NULL) return; while (*src != '\0') { *dest = *src; dest++; src++; } } /* strcat */ void strcat(char *dest, const char *src) { char *p = dest; int loc = 0; if (dest == NULL || src == NULL) return; while(*(p++) != '\0') loc++; while(*src != '\0') { dest[loc] = *src; src++; loc++; } } /* strlen */ int strlen(const char *str) { int len = 0; while (*str++ != '\0') len++; return len; } /* strcmp */ int strcmp(const char *s1, const char *s2) { char *ptr = s1 + strlen(s1); while (*s2 != '\0') { *ptr++ = *s2++; } *ptr = '\0'; return s1; } ``` ### Bitwise Manipulation > XOR Swap ```c= void swap(int *a, int *b) { *a = *a ^ *b; // ^ == XOR: 相同為 0,不同為 1 *b = *a ^ *b; *a = *a ^ *b; } ``` > 給定一個整型變量a,寫兩段代碼,第一個設置a的bit 3,第二個清除a 的bit 3。在以上兩個操作中,要保持其它位不變。 ```c= #define BIT3 (0x1 <<3) static int a; void set_bit3(void){ a |= BIT3; } void clear_bit3(void){ a &= ~BIT3; } /* set bit */ #define set_bit(b, n) ((b) |= ((n)<<1)) /* clear bit */ #define clear_bit(b, n) ((b) &= ~((n)<<1)) /* toggle bit */ #define toggle_bit(b, n) ((b) ^= ((n)<<1)) ``` > [Reverse Bits](https://leetcode.com/problems/reverse-bits/) ```c= ``` ### DSA > Complete binary search ```c= #include<stdio.h> #include<stdlib.h> void binary_search(int *arr, int len, int target){ int right = len - 1, left = 0, mid = 0; while (right >= left){ mid = left + (right - left)/2; if (target < arr[mid]){ right = mid - 1; } else if (target > arr[mid]) { left = mid + 1; } else { printf("found it. the index is %d", mid); return; } } } int main(){ // int a = 540, b = 840; int arra[] = {1, 2, 3, 4, 5}; int tgt = 4; binary_search(arra, sizeof(arra)/sizeof(int), tgt); getchar(); // To pause the program return 0; } ``` > Bubble sort ```c= void bubble_sort(int *arr, int len) { int tmp = 0; for (int i = 0; i < len; i++) { for (int j = i; j < len; j++) { if (arr[j] < arr[i]) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } }//for }// ```