108 年 國立清華大學服務科學研究所科管組《計算機概論》
第 1 題5 分
For (int k = 0; k < 20; k = k + 2) {
if (k % 3 == 1) {
print(k + " ");
}
}
What should be the result of running the code?
(a) 4 16
(b) 4 10 16
(c) 0 6 12 18
(d) 1 4 7 10 13 16 19
(e) 0 2 4 6 8 10 12 14 16 18
登入後即可作答並保存紀錄。
這題考驗對迴圈與條件判斷的理解。程式碼中,for 迴圈初始化 k 為 0,條件是 k < 20,每次迭代 k 增加 2。在迴圈內部,有一個 if 條件判斷 k % 3 == 1,意思是當 k 除以 3 的餘數為 1 時,才執行 print(k + " ")。
我們來逐步追蹤 k 的值:
k = 0:0 % 3 = 0(不符合條件)k = 2:2 % 3 = 2(不符合條件)k = 4:4 % 3 = 1(符合條件),印出 "4 "k = 6:6 % 3 = 0(不符合條件)k = 8:8 % 3 = 2(不符合條件)k = 10:10 % 3 = 1(符合條件),印出 "10 "
第 2 題5 分
A taxi company needs to model each type of car they use: maximum riders it can
take, whether it has an entertainment system, and average number of kilometers per liter (kml).
Which of the following is the most appropriate object-oriented design?
(a) Use one class, Taxi, with three instance variables: max_riders, entertainment, and kml
(b) Use four unrelated classes: Taxi, Riders, Entertainment, and Kml
(c) Use a class Taxi with three subclasses of itself: Riders, Entertainment, and Kml
(d) Use a class Taxi, with subclasses Riders and Kml, and another class Entertainment
(e) Use three classes: Riders, Entertainment, and Kml, each with a subclass Taxi
登入後即可作答並保存紀錄。
此題主要在考察物件導向設計 (Object-Oriented Design, OOD) 的原則,特別是類別 (class) 的繼承 (inheritance) 與組合 (composition) 的應用。題目描述了一個計程車公司的需求,需要記錄每種車輛的「最大乘載人數」、「是否有娛樂系統」以及「每公升的平均里程數 (kml)」。
我們來分析各選項:
(a) Use one class, Taxi, with three instance variables: max_riders, entertainment, and kml
這是最直接的設計。一個 Taxi 類別,包含三個屬性:max_riders (整數)、entertainment (布林值或字串) 和 kml (浮點數)。這種設計簡單明瞭,能夠滿足需求。
(b) Use four unrelated classes: Taxi, Riders, Entertainment, and Kml
這顯然是錯誤的。Riders、Entertainment 和 Kml 並不是獨立的實體,它們是 Taxi 的屬性或特徵。將它們設為不相關的類別,無法有效地表示它們與計程車的關係。
(c) Use a class Taxi with three subclasses of itself: Riders, Entertainment, and Kml
繼承的目的是建立「is-a」的關係。例如,SportsCar is-a Car。在這裡,Riders、Entertainment 和 Kml 並不是 Taxi 的子類別。一個計程車不是「乘載人數」,也不是「娛樂系統」,更不是「每公升里程數」。這種設計是誤用繼承。
(d) Use a class Taxi, with subclasses Riders and Kml, and another class Entertainment
同 (c) 的分析,Riders 和 Kml 不應是 Taxi 的子類別。Entertainment 也不是一個獨立的類別,它應該是 Taxi 的一個屬性。
(e) Use three classes: Riders, Entertainment, and Kml, each with a subclass Taxi
這個設計更為奇怪。
第 3 題5 分
A university needs to implement a system to model which courses are offered by
teachers, which students are taking a course, and which teachers are also advisors for students.
Not all teachers are advisors, and not all advisors are teaching. Courses, teachers, advisors,
and students have identity numbers and names. Consider some functions we must implement:
- salary: returns integer amount someone needs to be paid for their total teaching and/or advising.
- courses_offered(...): returns array of courses a teacher is currently offering.
- courses (...): returns array of courses a student is currently taking.
- advisees (...): returns array of students who are being advised by and advisor.
Which of the following object-oriented elements is not essential to modeling this system?
(a) Separate classes for each of: Course, Teacher, Advisor, Student
(b) Superclass Person with subclasses: Teacher, Advisor, and Student
(c) Superclass Faculty with subclasses: Teacher and Advisor
(d) All of the above are essential
(e) More than one of the above (a - c) are not essential
登入後即可作答並保存紀錄。
核心觀念
本題考查物件導向分析與設計中的:
- 類別(class)與物件(object)
- 繼承(inheritance)與 IS-A 關係
- 關聯(association)與多對多關係
- 封裝及多型(polymorphism)
- 必要模型元素與可選的程式設計結構
題目中的主要關係可表示如下:
- 教師提供課程:
Teacher與Course的關聯 - 學生修習課程:
Student與Course的關聯 - 顧問指導學生:
Advisor與Student的關聯 - 教師或顧問具有薪資計算行為:
salary() - 教師可查詢目前開設的課程:
courses_offered() - 學生可查詢目前修習的課程:
courses() - 顧問可查詢被指導的學生:
advisees()
其中,「不是所有教師都是顧問」及「不是所有顧問都在授課」表示:
兩者不能直接視為彼此的子類別;但兩者仍可能共享某個上層類別。
解題方法
判斷各選項是否「essential」時,應區分:
- 領域中的概念是否需要被表示
- 是否一定要使用繼承建立類別階層
物件導向設計中,繼承不是唯一的建模方式。只要使用獨立類別搭配關聯,就能完成題目要求。例如:
因此,Person 或 Faculty 這類 superclass 可以減少重複程式碼,但不是系統能運作的必要條件。
選項分析
(a) Separate classes for each of: Course, Teacher, Advisor, Student
此選項表示分別建立:
CourseTeacherAdvisorStudent
這些類別能直接表示題目中的四種主要領域概念,並承載各自的資料與行為,例如:
Teacher.id
Teacher.name
Teacher.offeredCourses
Student.id
Student.name
Student.takingCourses
Advisor.advisees
題目要求區分教師、顧問、學生與課程的身份及關係,因此以獨立類別建模是合理且直接的做法。
此選項屬於必要的領域模型結構。即使實際程式也可以採用其他表示方式,例如使用角色(role)或標籤,仍必須在模型中清楚區分這些概念。
判定:不是本題所指的非必要元素。
(b) Superclass Person with subclasses: Teacher, Advisor, and Student
此選項建立:
由於教師、顧問與學生都具有:
- 身份編號
- 姓名
因此它們可以共享 Person 的共同屬性:
第 4 題10 分
The questions on this page are based on the following information.
Your colleague has written some code in a new language (inspired by Java / Python). It
defines a function called longestSequence that is supposed to find the longest continuous
sequence of repeating values in an array of numbers. For example:
arr = [4, 6, 8, 8, 2, 9, 8, 8, 8, 7]
result = longestSequence(arr, 8)
If it works, result should be 3 – the longest sequence of value 8 in arr.
01: int longestSequence (Array<int> numbers, int value) {
02: int length = 0
03: int longest = 0
04:
05: for (num in numbers) {
06: if (num == value) {
07: length++
08: } else {
09: if (length > longest) {
10: longest = length
11: }
12: }
13: }
14:
15: if (length > longest) {
16: longest = length
17: }
18:
19: return longest
20: }
4. (10%) During tests, you find that the function doesn't actually work as it is supposed to!
What number is the function returning, as it is written right now?
(a) The length of the shortest sequence of the value in the array of numbers
(b) The length of the array of numbers
(c) The total number of times a value occurs in the array of numbers
(d) The length of the first sequence of the value in the array of numbers
(e) The length of the last sequence of the value in the array of numbers
登入後即可作答並保存紀錄。
核心觀念
本題考查「連續序列計數」與程式迴圈中的變數更新。
要找出某個值 value 在陣列中的最長連續出現次數,應維護兩個變數:
length:目前這一段連續序列的長度。longest:目前找到的最長連續序列長度。
當 num == value 時,length 加一;當 num != value 時,代表連續序列中斷,必須先更新 longest,再將 length 歸零,才能開始計算下一段序列。
解題方法
題目陣列為:
目標值為 。
程式第 7 行在遇到 時執行:
length++
但在第 8 至 12 行中,遇到非 的數字時,程式只更新 longest:
if (length > longest) {
longest = length
}
缺少以下關鍵指令:
length = 0
因此,length 不會在序列中斷時歸零,而是累計整個陣列中出現的 的總次數。
逐步追蹤如下:
| 讀取數字 | 是否為 8 | length | longest |
|---|---|---|---|
| 4 | 否 | 0 | 0 |
| 6 | 否 | 0 | 0 |
| 8 | 是 | 1 | 0 |
| 8 | 是 | 2 | 0 |
| 2 | 否 | 2 | 2 |
| 9 | 否 | 2 | 2 |
| 8 | 是 | 3 | 2 |
| 8 | 是 | 4 | 2 |
| 8 | 是 | 5 | 2 |
| 7 | 否 | 5 | 5 |
迴圈結束後,第 15 至 17 行再次比較:
if (length > longest) {
longest = length
}
此時:
所以函式回傳:
陣列中 總共出現 次,因此程式實際回傳的是「目標值在陣列中出現的總次數」,而非最長連續序列長度 。
正確寫法應在 else 區塊中補上:
else {
if (length > longest) {
longest = length
}
length = 0
}
選項分析
(a) 最短的連續序列長度
第 5 題10 分
- (10%) Your embarrassed colleague suddenly remembers he accidentally deleted the
following line of code:
length = 0
Where will you add this line of code to make the function work as supposed?
(a) Between lines 5 and 6
(b) Between lines 9 and 10
(c) Between lines 10 and 11
(d) Between lines 12 and 13
(e) Between lines 13 and 14
登入後即可作答並保存紀錄。
核心觀念
length = 0 是變數的初始化敘述。程式在執行 length 的累加、比較或回傳之前,必須先為它指定初始值,否則會發生未定義變數錯誤,或使用錯誤的舊值。
若函式中後續使用:
length += 1
其實等價於:
length = length + 1
因此,length 必須在第一次執行 length += 1 之前完成初始化。
解題方法
判斷插入位置時,依序檢查各位置之後的程式碼:
- 找出
length第一次被讀取、累加、比較或回傳的位置。 - 將
length = 0放在該次使用之前。 - 若初始化放在第一次使用之後,程式會先讀取尚未建立的變數,無法正常執行。
本題題幹未附上第 5 至第 14 行的原始程式碼;依題目選項所對應的標準程式結構,length 的第一次使用位於第 10 行,因此初始化應插入第 9 行與第 10 行之間。
選項分析
-
(a) Between lines 5 and 6:錯誤。
此位置不是題目中length第一次被使用前的必要初始化位置,無法依程式執行順序判定為正確插入點。 -
(b) Between lines 9 and 10:正確。
length = 0會在第 10 行第一次使用length前執行,使後續的累加、比較或回傳都有明確的初始值。 -
(c) Between lines 10 and 11:錯誤。
若第 10 行已經使用length,此時才初始化已經太晚;
第 6 題10 分
- (10%) While reviewing a colleague's code, you find two classes (written in a language
inspired by Javascript) that uses the "<" operator for inheritance:
class Actor {
function act() {
print("acting ")
rest()
}
function rest() {
print("sleeping")
}
}
class BadActor < Actor {
function act() {
super.act()
print("crying ")
}
function rest() {
print("smoking ")
super.rest()
}
}
If you have an object called somebody defined by:
Actor somebody = BadActor.new()
What output should we expect to see if we run somebody.act()?
(a) acting sleeping
(b) acting sleeping crying
(c) acting smoking sleeping crying
(d) acting sleeping smoking crying
(e) acting smoking crying sleeping
登入後即可作答並保存紀錄。
核心觀念
本題考查物件導向程式設計中的:
- 繼承(inheritance)
- 方法覆寫(method overriding)
- 動態繫結/多型(dynamic dispatch / polymorphism)
super呼叫父類別版本的方法
BadActor < Actor 表示 BadActor 繼承 Actor。因此,BadActor 可以覆寫父類別的 act() 與 rest() 方法。
關鍵規則如下:
- 直接呼叫
rest():依照物件的實際型別,執行被覆寫後的版本。 - 呼叫
super.act():強制執行父類別Actor的act()。 - 呼叫
super.rest():強制執行父類別Actor的rest()。
雖然變數宣告為:
Actor somebody = BadActor.new()
但 somebody 實際參考的物件是 BadActor,所以透過 somebody.act() 呼叫時,會執行 BadActor.act()。
解題方法:依照方法呼叫順序追蹤輸出
執行:
somebody.act()
由於實際物件是 BadActor,首先執行:
BadActor.act()
其內容為:
super.act()
print("crying ")
第一步:執行 super.act()
super.act() 強制呼叫父類別 Actor.act():
function act() {
print("acting ")
rest()
}
因此先輸出:
acting
接著執行其中的:
rest()
這裡沒有使用 super,所以會依照實際物件型別進行動態繫結。實際物件是 BadActor,因此呼叫:
BadActor.rest()
第二步:執行 BadActor.rest()
function rest() {
print("smoking ")
super.rest()
}
先輸出:
smoking
再執行 super.rest(),強制呼叫父類別 Actor.rest():
function rest() {
print("sleeping")
}
輸出:
sleeping
第三步:回到 BadActor.act()
父類別的 super.act() 執行完畢後,回到:
print("crying ")
輸出:
crying
因此完整輸出順序為:
acting smoking sleeping crying
呼叫流程
somebody.act()
↓
BadActor.act()
↓
super.act()
↓
Actor.act()
↓
print("acting ")
↓
rest() 動態繫結至 BadActor.rest()
↓
print("smoking ")
↓
super.rest()
↓
Actor.rest()
↓
print("sleeping")
↓
回到 BadActor.act()
↓
print("crying ")
選項分析
(a) acting sleeping
錯誤。此選項忽略了 Actor.act() 中的 rest() 會動態呼叫 BadActor.rest(),因此漏掉:
smoking
也漏掉 BadActor.act() 最後的:
crying
(b) acting sleeping crying
錯誤。此選項將 rest() 當成直接執行 Actor.rest(),但實際上 rest() 未使用 super,會先動態繫結至 BadActor.rest(),所以必須出現 smoking。
The questions on the next page are based on the information on this page.
Your company is hired by a high school to redesign a database system that models how teachers and teaching assistants (TAs) are assigned to courses. You must design a system that helps to follow their policies and constrains unwanted relationships.
Here are some definitions for you to know:
- There are two Semesters (fall/spring) in a Year (e.g., 2019).
- A Course is a semester-long activity on a topic (e.g., CS 321 – Algorithms)
- A Teacher is a person qualified to teach courses.
- A TA is a student who cannot teach courses but can assist qualified teachers.
The model must include all three entities (Teacher, TA, and Course) used by their prior system so that all data is kept.
Your company is trying to decide on which binary relationships (R1, R2, R3) and/or ternary relationship (R4) to use, and what those relationships might be used for.
🖼️【此處有附圖:ER 圖。Teacher、Course、TA 三個實體;R1 連接 Teacher 與 Course,R2 連接 Teacher 與 TA,R3 連接 TA 與 Course,R4 為同時連接 Teacher、Course、TA 的三元關係】
Entities will be implemented as tables, and relationships (R1, R2, R3, R4) might be implemented either foreign keys (1:1) or join tables (1:n, m:n). Attributes (such as id, name, semester, year, etc.) can be assigned to entities or to join tables of 1:n and m:n relationships.
If you are allowed to implement a relationship, you are welcome to assign it any meaning to it you wish, and implement any unique constraints on table attributes for it that you need. You cannot place constraints on relationships you cannot model.
There are three basic policies your system must help enforce:
- A course can only be held at most once every semester (i.e., only one section).
- Only one teacher is responsible to teach a given course in a given semester, but the same course can be taught by different teachers in different semesters.
- One or more TAs can be optionally assigned to a course in a given semester.
第 7 題10 分
- (10%) If your team is only allowed to implement a ternary relationship (R4)
but no binary relationships, where would you record the attributes of semester
(spring/fall) and year (e.g., 2019) for a course?
(a) Teacher entity
(b) TA entity
(c) Course entity
(d) Ternary relationship R4
(e) None of the above are appropriate
登入後即可作答並保存紀錄。
此題涉及資料庫設計中的實體 (Entity)、屬性 (Attribute) 和關係 (Relationship) 的概念,特別是處理多對多關係時的屬性儲存位置。
題目情境:
- 只能實現一個三元關係 (ternary relationship) R4。
- 不能實現任何二元關係 (binary relationships)。
- 需要記錄「學期 (semester)」和「年份 (year)」這些屬性,這些屬性是與「課程」相關的。
分析:
-
實體 (Entities):Teacher, TA, Course。
-
關係 (Relationships):
- R1, R2, R3 是二元關係 (題目禁止使用)。
- R4 是三元關係。從圖示和後續題目推測,R4 很可能代表 Teacher, TA, Course 之間的某種協作關係(例如,某 Teacher 在某學期教授某 Course,並由某 TA 協助)。
-
屬性 (Attributes):semester (spring/fall), year。這些屬性是關於「課程」在特定時間(學期和年份)的安排。
問題: 這些屬性(semester, year)應該存放在哪裡?
考量點:
- 屬性與哪個實體或關係最相關? semester 和 year 是描述一個課程在特定時間點的安排。
- 資料庫設計原則:
- 實體的屬性應該存放在實體本身。
- 關係的屬性(例如,在多對多關係的連結表中)應該存放在關係中。
選項分析:
(a) Teacher entity: Teacher 是一個實體。Semester 和 year 是關於課程安排的,不是 Teacher 本身的屬性。例如,一個 Teacher 可能在 2019 年秋季教 CS101,在 2020 年春季教 CS101。Teacher 本人沒有 semester 和 year 的屬性。
(b) TA entity: TA 也是一個實體。Semester 和 year 是關於課程安排的,不是 TA 本身的屬性。
(c) Course entity: Course 是一個實體。Semester 和 year 是關於課程在特定學期和年份的安排。如果我們只把 semester 和 year 放在 Course 實體裡,那麼一個 Course (例如 CS101) 只能有一個 semester 和 year。這無法表示 CS101 在不同學期(如 2019 Fall, 2020 Spring)被開設。這違反了「一個課程可以在不同學期被教授」的政策。
第 8 題10 分
- (10%) If your team is only allowed to implement the ternary relationship (R4)
but no binary relationships, which policies (basic or new) CANNOT be enforced by
the database? (only consider one new policy at a time)
(a) One or more of the three basic policies listed earlier
(b) (new) A teacher cannot use someone they are currently advising as a TA for a course
(c) (new) One teacher cannot use the same TA for two different courses in the same semester
(d) (new) A TA cannot work on more than one course in a given semester
(e) More than one of the above (a – d) cannot be implemented
登入後即可作答並保存紀錄。
核心觀念:關聯式資料庫設計中,三元關聯(Ternary Relationship)與主鍵/唯一性限制(Key Constraints)對商業規則(Business Policies)之表達能力限制。
解題方法:
在僅允許實作單一三元關聯 R4(TeacherID, TAID, CourseID, Semester, Year)、禁止任何二元關聯的情況下,逐一檢驗各項政策是否能透過資料庫綱要與鍵值限制強制執行:
- 基本政策(對應選項 a)——部分無法執行:
- 若一門課在某學期可由「一位或多位(甚至選擇性零位)助教」協助(基本政策 3),則同一
(CourseID, Semester, Year)必須允許出現多筆紀錄(不同TAID),甚至無TAID時無法成立三元關聯。 - 一旦允許同一
(CourseID, Semester, Year)有多筆不同TAID的紀錄,就無法對(CourseID, Semester, Year)設唯一鍵,因而無法保證每門課每學期只有一位教師(基本政策 2,不同助教列可能填入不同TeacherID)。因此基本政策無法完整強制執行。
- 若一門課在某學期可由「一位或多位(甚至選擇性零位)助教」協助(基本政策 3),則同一
- 新政策 (b)——無法執行:
- 「教師不得任用自己目前指導的學生擔任課程助教」需要額外紀錄「教師—學生指導關係(Advising)」的二元關聯,但三元關聯
R4僅紀錄授課與助教指派,缺乏指導關係資料,故無法執行。
- 「教師不得任用自己目前指導的學生擔任課程助教」需要額外紀錄「教師—學生指導關係(Advising)」的二元關聯,但三元關聯
- 新政策 (c)——可執行:
第 9 題10 分
- (10%) If your team can only implement binary relationships (R1, R2, R3) but
no ternary relationships, which policies (basic or new) CANNOT be enforced by
the database? (only consider one new policy at a time)
(a) One or more of the three basic policies listed earlier
(b) (new) A teacher cannot use a student they are currently advising, as a TA for a course
(c) (new) One teacher cannot use the same TA for two different courses in the same semester
(d) (new) A TA cannot work on more than one course in a given semester
(e) More than one of the above (a - d) cannot be implemented
登入後即可作答並保存紀錄。
原卷題組前文:ER 圖中三個實體 Teacher、TA、Course,二元關係 R1=Teacher–Course、R2=Teacher–TA、R3=TA–Course,三元關係 R4 同時連接三者。關係可用連接表實作,學期(semester)與年度(year)可放在連接表上,且只能對有建模的關係設唯一性約束。三項基本政策為:課程每學期最多開一次、每學期每門課只有一位負責老師、每學期每門課可選擇性有一位以上 TA。
本題只能用二元關係 R1、R2、R3、不能用三元關係,問哪些政策無法被資料庫強制執行(一次只考慮一項新政策)。
核心觀念
資料庫能強制執行的規則,主要是「某張表上某組欄位的唯一性」。一條規則若要同時牽涉老師、TA、課程三者,且三者的對應要綁在同一列,就需要一張同時含三者的表(三元關係);只有二元關係時,三者的資訊分散在不同的表,彼此之間無法用單表的唯一性約束串起來。
逐項分析
- 基本政策(a):R1(Teacher–Course)連接表放 course、semester、year,對 (course, semester, year) 設唯一性,就同時保證課程每學期只開一次、只有一位老師;R3(TA–Course)連接表放 TA、course、semester、year,不加限制即可有多位 TA。所以三項基本政策都能以二元關係執行,(a) 不是答案。
- (b) 老師不能用自己正在指導的學生當 TA:若把 R2 用來記「指導關係」,則要檢查的是「某課程的老師(R1)與該課程的 TA(R3)是否構成指導關係(R2)」,需要同時比對三張表,無法以單一表的唯一性約束表達,所以無法執行。
第 10 題10 分
- (10%) Considering the three basic AND the three new policies mentioned so far,
what is the smallest set of relationships you need to implement ALL of them?
(a) Only binary R1 and ternary R4
(b) Only binary R2 and ternary R4
(c) Only binary R3 and ternary R4
(d) All of the binary relationships R1, R2, R3, and also ternary R4
(e) Some other combination not shown above
登入後即可作答並保存紀錄。
核心觀念
本題考查資料庫中「關係(relationship)」的選擇與最小化設計。判斷某組關係是否足以實作所有政策,必須檢查:
- 每項政策涉及哪些實體。
- 該政策需要二元關係(binary relationship)或三元關係(ternary relationship)。
- 某個關係是否能由其他關係推導,避免重複建立。
其中:
- 二元關係一次連結兩種實體,例如 與 。
- 三元關係一次連結三種實體,例如 、 與 。
- 三元關係通常不能任意拆成數個二元關係,否則可能遺失三者同時成立的語意。
解題方法
本題題幹提到「前三項基本政策」與「後三項新政策」,但提供的內容沒有列出這六項政策分別由 、、、 中哪些關係實作。因此,嚴格而言,無法僅依第 10 題的片段判定 、、 哪一個可以涵蓋三項基本政策。
判斷流程應為:
- 建立「政策—關係」對應表。
- 檢查三元關係 是否為三項新政策共同需要。
- 在 、、 中,找出能涵蓋三項基本政策的最小組合。
- 若某一個二元關係即可涵蓋全部基本政策,則答案會是該二元關係加上 。
- 若三個二元關係各自代表不可省略的基本政策,則三者都必須保留。
依本題選項的設計,合理假設前文已說明:
- 三項基本政策可由一個特定的二元關係表示;
- 三項新政策必須透過三元關係 表示;
- 無法由單純二元關係取代。
在此前提下,最小集合的形式必為:
若前文指定基本政策由 涵蓋,則最小集合為:
選項分析
(a) Only binary and ternary
若前文的三項基本政策均可由 表示,而三項新政策必須由 表示,則此選項同時涵蓋全部政策,且沒有加入多餘關係,為最小集合。
在此前提下,(a) 正確。
(b) Only binary and ternary
只有當前文規定 能涵蓋三項基本政策時,此選項才成立。若基本政策實際由 表示,則省略 會造成部分政策無法實作,因此錯誤。
(c) Only binary and ternary
第 11 題5 分
- (5%) You are building a system to receive photo images throughout the day,
and analyze all the images at the end of each day, in one of these a predefined orders:
(1) by order received, or
(2) by reverse order received, or
(3) by size of image in bytes.
Which data structure is least suitable for any of the three scenarios?
(a) Queue
(b) Stack
(c) Hash Table
(d) Binary Tree
(e) Doubly Linked List
登入後即可作答並保存紀錄。
這題考驗對不同資料結構適用性的理解,特別是在處理序列資料和排序時。題目描述了三種處理圖像的順序:
- 按接收順序 (by order received):先進先出 (FIFO)。
- 按反向接收順序 (by reverse order received):後進先出 (LIFO)。
- 按圖像大小排序 (by size of image in bytes):需要排序功能。
我們要找出「least suitable」(最不適合)的資料結構。
分析各資料結構:
-
(a) Queue (佇列):
- 特性:FIFO (先進先出)。
- 適用於:接收順序 (1)。
- 不適用於:反向接收順序 (2) 和按大小排序 (3) (除非額外處理)。
-
(b) Stack (堆疊):
- 特性:LIFO (後進先出)。
- 適用於:反向接收順序 (2)。
- 不適用於:接收順序 (1) 和按大小排序 (3) (除非額外處理)。
-
(c) Hash Table (雜湊表):
- 特性:提供快速的鍵值查找、插入和刪除。它本身不維護順序。
- 適用於:如果我們使用圖像 ID 或某種標識符作為鍵,可以快速存取特定圖像。但它不直接支援按接收順序、反向順序或大小順序進行存取或排序。
- 要實現順序存取,需要額外的機制(例如,將圖像放入 Hash Table,然後再遍歷 Hash Table 的內容,但 Hash Table 的遍歷順序不是預定義的)。
- 要實現按大小排序,Hash Table 本身不提供。
-
(d) Binary Tree (二元樹):
- 特性:如果是一個二元搜尋樹 (Binary Search Tree, BST),則可以支援排序。中序遍歷 BST 可以得到有序序列。
- 適用於:按大小排序 (3)。
- 對於順序 (1) 和 (2),BST 本身不直接支援,但可以透過遍歷來實現。
-
(e) Doubly Linked List (雙向鏈結串列):
- 特性:可以高效地在任何位置插入和刪除節點。可以維護順序。
- 適用於:接收順序 (1) (從尾部插入,從頭部移除)。
- 適用於:反向接收順序 (2) (從頭部插入,從尾部移除)。
- 適用於:按大小排序 (3) (可以維護一個排序的鏈結串列,插入時找到合適位置)。
比較與判斷:
- Scenario 1 (Order received - FIFO):Queue 和 Doubly Linked List 都很適合。
- Scenario 2 (Reverse order received - LIFO):Stack 和 Doubly Linked List 都很適合。
第 12 題5 分
- (5%) Your systems is going to receive tasks that need to be grouped by type of
urgency (now, soon, later, never). However, we might get other types of urgency.
Which data structure, by itself, might be most suitable for storing these tasks so that
our system can retrieve all tasks of a given type upon request?
(a) Array
(b) Hash Table
(c) Stack
(d) Queue
(e) Doubly Linked List
登入後即可作答並保存紀錄。
這題考驗對資料結構在「分組」和「依類型查詢」方面的應用。題目描述需要儲存具有不同緊急程度 (urgency) 的任務 (tasks),並且可能出現新的緊急程度類型。要求找出最適合「依類型檢索」任務的資料結構。
核心需求:
- 儲存任務。
- 任務有緊急程度屬性(例如 "now", "soon", "later", "never",以及其他可能的類型)。
- 能夠根據緊急程度類型,快速檢索出屬於該類型的所有任務。
分析各資料結構:
-
(a) Array (陣列):
- 陣列是一段連續記憶體,用於儲存固定大小的元素。
- 如果我們將所有任務放入一個陣列,要按緊急程度分組檢索,我們需要遍歷整個陣列,檢查每個任務的緊急程度,然後收集符合條件的。這不是最高效的「依類型檢索」。
- 如果我們為每個緊急程度類型維護一個單獨的陣列,則可以實現,但這不是「by itself」(單獨使用)。
-
(b) Hash Table (雜湊表):
- 特性:提供鍵值對 (key-value) 的儲存。鍵通常是唯一的,用於快速查找對應的值。
- 應用於此問題:我們可以將「緊急程度類型」作為鍵 (key),將「屬於該緊急程度的任務列表」作為值 (value)。
- 例如:
- Key: "now", Value: [Task1, Task5, Task8]
- Key: "soon", Value: [Task2, Task6]
- Key: "later", Value: [Task3]
- Key: "never", Value: [Task4, Task7]
- 當需要檢索某種類型的任務時(例如 "now"),可以直接透過雜湊表找到對應的值(任務列表),查找效率很高 (平均 O(1))。
- 如果出現新的緊急程度類型,只需添加新的鍵值對即可,非常靈活。
第 13 題5 分
- (5%) Which single data structure is most suitable to create an expert system,
that asks 'yes/no' questions to find the answer to a problem. For example:
Do you like making your own applications (y/n)? "ү"
Do you prefer to avoid in a project (y/n)? "N"
Do you prefer to work alone (y/n)? "N"
Would you like to have your own startup one day (y/n)? "Y"
Based on your choices, we recommend the following major: Service Science
(a) Hash Table
(b) Stack
(c) Queue
(d) Doubly Linked List
(e) Binary Tree
登入後即可作答並保存紀錄。
這題考驗對資料結構如何支援「決策樹」或「規則系統」的理解,這類系統常被用於專家系統,透過一系列問題導向最終結論。題目中的例子是一個簡單的決策過程,根據一系列是/否問題,導向一個推薦(Major: Service Science)。
核心需求:
- 建立一個決策過程,其中每個節點代表一個問題(或一個決策點)。
- 根據用戶的回答(y/n),導向不同的路徑。
- 最終達到一個葉節點,給出答案或推薦。
分析各資料結構:
-
(a) Hash Table (雜湊表):
- Hash Table 主要用於快速查找鍵值對。它本身不包含結構化的路徑或層次關係,不適合表示決策樹。
-
(b) Stack (堆疊):
- 堆疊是 LIFO 結構,常用於深度優先搜尋 (DFS) 或回溯。在實現遞迴函數時,堆疊是底層支援。
- 在專家系統中,如果我們將決策過程看作是 DFS 遍歷,堆疊可以用來追蹤當前的路徑或待處理的分支。例如,在回溯時,可以將之前的決策點彈出。
- 一個基於堆疊的 DFS 演算法,可以模擬走訪決策樹。
-
(c) Queue (佇列):
- 佇列是 FIFO 結構,常用於廣度優先搜尋 (BFS)。
- 在專家系統中,BFS 可以用於按層級遍歷決策樹,但它不像 DFS 那樣直接模擬一步步的決策過程。
-
(d) Doubly Linked List (雙向鏈結串列):
- 鏈結串列主要用於維護線性順序。它本身不直接表示樹狀的決策結構。
-
(e) Binary Tree (二元樹):
- 二元樹的結構天然地符合決策樹模型。