
台積電的主要面試流程分為:
- 資歷審核
- HackerRank 測驗
- 主管面試
- 實廠適性、英文測驗
- HR面試
接下來就是分享一下Hackerrank OA程式測驗的真題。
1. Group Division
A university has admitted a group of n
students with varying skill levels. To better accommodate the students, the university has decided to create classes tailored to these skill levels. A placement examination returns a skill level for each student, represented by an array levels[]
, where levels[i]
denotes the skill level of student i
.
All students within a group must have skill levels that are within a specified range, maxSpread
, of one another. The goal is to determine the minimum number of groups that must be formed to ensure that no group contains students whose skill levels differ by more than maxSpread
.
Example
Input:
n = 5
(number of students)levels = [1, 4, 7, 3, 4]
(skill levels of the students)maxSpread = 2
(the maximum allowed skill level difference within a group)
Output:
3
(minimum number of groups)
In this example, one optimal grouping is:
- Group 1: (1, 3)
- Group 2: (4, 4)
- Group 3: (7)
An alternative valid grouping could be:
- Group 1: (1)
- Group 2: (3, 4, 4)
- Group 3: (7)
In both cases, we form 3 groups, and this is the minimum number of groups that can be formed. There’s no way to form fewer than 3 groups given the maxSpread
condition.
解題思路
- 排序技能水准:首先將學生的技能水准從小到大排序。 這樣可以確保我們在遍歷學生時,只需要關注相鄰學生的技能差异。
- 貪心策略分組:遍歷排序後的學生清單,嘗試將每個學生放入當前組,直到當前組內的最大技能差异超過maxSpread。 如果超過了,就開始新的一組。
2. Linked List Creation
There is a singly linked list of n
nodes. Each node is an instance of a SinglyLinkedListNode
, which has an integer value data
and a pointer to the next node, next
. Perform the following operations to generate a new linked list:
- Select all the nodes at odd positions.
- Append these nodes to the new linked list, keeping them in their original order.
- Delete these nodes from the old linked list.
- Repeat from step 1 until there are no nodes left in the old linked list.
Return a reference to the head of the final linked list.
Note: Extra memory, other than creating some new nodes, should not be used for the implementation.
解題思路
- 定義鏈表節點結構:每個鏈表節點包含data(節點值)和next(指向下一個節點的指針)。
- 選取奇數位置節點:我們可以通過遍歷鏈表來獲取奇數位置的節點。 奇數位置的節點是指1、3、5等位置的節點。
- 修改指針連接:
- 從原鏈表中獲取奇數位置的節點,並將它們逐一添加到新鏈表的末尾。
- 删除這些節點時,必須確保將原鏈表的前驅節點正確地指向下一個節點,避免斷鏈。
- 重複操作:每次處理完奇數位置的節點後,需要跳過一個節點(即删除當前處理的節點的下一個節點),並繼續執行此操作,直到鏈表為空。
3. String compression and decompression
Various compression methods are employed to minimize the size of messages transmitted over the internet. A specific algorithm compresses a given string by indicating the total number of consecutive occurrences of each character. For instance, one string "aabbaa"
can be compressed as "a2b2a2"
. The characters of each character are grouped as follows:
a
occurs two times consecutively,b
occurs two times consecutively,a
occurs two times consecutively.
If a character occurs only once, it is added to the compressed string. If it occurs consecutively, the character is added to the string followed by an integer representing the number of consecutive occurrences. Thus, the compressed form of the string is "a2b2a2"
.
Function Description:
Complete the function compressedString
in the editor below. The function must return the compressed form of the message.
compressedString has the following parameter(s): message
: a string.
Returns: string
: the compressed message.
def compressedString(s):
# Write your code here
n = len(s)
ans = ""
i = 0
while i < n:
j = i + 1
while j < n and s[j] == s[i]:
j += 1
ans += s[i]
if j - i > 1:
ans += str(j - i)
i = j
return ans
解題思路是遍歷字串,對於每個字符,統計其連續出現的次數,並將字符與次數合併生成壓縮字串。
Learn More
Tsmc一畝三分地
台積電人才招聘
台積電面試懶人包:面試流程、面試問題 TSMC IT Careers
如果你也需要我們的台積電hackerrank作弊服務和面試輔助、代面試服務,請 立即聯繫我們。