Back to Final Review Hub 返回期末复习中心
Day 5 - Final Review Day 5 - 期末复习

🎯 Exam Problem-Solving Strategies 🎯 考试解题策略

Master the 5-Step Method, Question Types & HTTP Status Codes 掌握5步解题法、题型分类与HTTP状态码

📊 Question Type Classification Matrix 📊 题型分类矩阵

Understanding question types is the first step to exam success. Each type has distinct characteristics and focus areas. 理解题型是考试成功的第一步。每种类型都有独特的特征和考点。

Type类型 Characteristics特征 Key Focus Areas考点 Example题号示例
Pure Function纯函数 No HTTP, no server, only input/output无HTTP、无server、只有输入输出 Array ops, string handling, file I/O, logic数组操作、字符串处理、文件I/O、逻辑判断 Q23
REST APIREST API Express routes, swagger.yamlExpress routes、swagger.yaml HTTP status codes, routing, req/res handlingHTTP状态码、路由处理、请求响应 Q24
Business Logic业务逻辑 Complex conditions, notification systems复杂条件判断、notification系统 State machines, array search, string matching状态机、数组查找、字符串匹配 Q25
Testing测试 .test.ts files.test.ts文件 Jest, expect assertions, edge casesJest、expect、edge case All questions各题都有

🚀 Universal 5-Step Problem-Solving Method 🚀 通用解题流程(5步法)

1 Read & Understand Interface (5 min)读题 + 理解接口 (5分钟)
  • Check interface.ts - What data structures are defined?interface.ts - 定义了什么数据结构
  • Check swagger/test - What inputs/outputs are required?看 swagger/test - 需要什么输入输出
  • Draw a simple data flow diagram画个简单的数据流图
2 Design Core Logic (5-10 min)设计核心逻辑 (5-10分钟)
  • Write pseudocode/comments - don't write actual code yet写伪代码/注释 - 不要写具体代码
  • Identify edge cases: empty array, null, boundary values识别edge case: 空数组、null、边界值
  • Decide data structure: Map? Array? Object?决定数据结构: Map? Array? Object?
3 Implement Core Functions (15-20 min)实现核心函数 (15-20分钟)
  • Write main logic first, ignore error handling先写主逻辑,不考虑错误处理
  • Handle normal cases first处理正常case
  • Gradually add error handling逐步添加错误处理
4 HTTP Layer Wrapper (if REST) (10 min)HTTP层包装 (如果是REST题) (10分钟)
  • Set up routes: GET/POST/PUT/DELETE设置route: GET/POST/PUT/DELETE
  • Extract data from req: req.body / req.params / req.query从req取数据: req.body / req.params / req.query
  • Call core functions and return correct status codes调用核心函数,返回正确状态码
5 Test & Adjust (5 min)对照测试调整 (5分钟)
  • Run tests, see what fails跑测试,看哪里fail
  • Fix one by one逐个fix
  • Check spelling, types, status codes检查拼写、类型、状态码

🔧 Q23 Type - Pure Function (e.g., Cockroaches) 🔧 Q23类型 - 纯函数 (如 Cockroaches)

Characteristics:特征: Only function signature + interface, no Express/server, pure input→process→output 只有函数签名 + interface,没有Express/server,纯输入→处理→输出

Step 1: Read Interface Step 1: 看interface

  • Input type? string[] (file paths)输入是什么?string[] (文件路径数组)
  • Output type? DecontaminateResult输出是什么?DecontaminateResult
  • Intermediate data? locations array需要什么中间数据?locations数组

Code Template代码模板

async function decontaminate(files: string[]): Promise<DecontaminateResult> { const result = { attic: 0, bathroom: 0, bedroom: 0, kitchen: 0 }; for (const file of files) { const content = await fs.promises.readFile(file, 'utf-8'); const locations = content.split('\n').filter(Boolean); for (const loc of locations) { if (loc in result) { result[loc as keyof typeof result]++; } } } return result; }
Key Points:考点:
  • fs.readFileSync vs fs.promises.readFile
  • async/await
  • Array operations: split, filter, map数组操作: split, filter, map
  • TypeScript type definitionsTypeScript类型定义

📋 HTTP Status Codes Quick Reference (MUST MEMORIZE!) 📋 HTTP状态码速查表 (必须记住!)

200
OK

Success - Request completed成功 - 请求完成

400
Bad Request

Client input error客户端输入错误

401
Unauthorized

Not authenticated未认证

403
Forbidden

Access denied禁止访问

404
Not Found

Resource not found找不到

500
Server Error

Internal server error服务器内部错误

Key Distinction:关键区分:
  • 400: Client made a mistake (bad format, missing field)客户端错了(格式错误、缺少字段)
  • 401: "Who are you?" - Authentication required"你是谁?" - 需要认证
  • 403: "I know who you are, but you can't do this""我知道你是谁,但你不能做这个"
  • 404: Resource doesn't exist资源不存在

🌐 Q24 Type - REST API (e.g., Brooms) 🌐 Q24类型 - REST API (如 Brooms)

Characteristics:特征: swagger.yaml defines API, Express server framework, multiple endpoints + error handling swagger.yaml定义API,Express server框架,多个endpoint + 错误处理

Step 1: Read swagger.yaml (MOST IMPORTANT!)Step 1: 读swagger.yaml (最重要!)

Endpoint Method Parameters参数 Returns返回
/clear DELETE none {}
/user POST header + body {userId}
/message POST body {msgId}
/message/{from}/{to} GET path + query {msgs}
/message/{id} PUT path + body {msg}

Step 2: Design Data StorageStep 2: 设计数据存储

interface DataStore { users: User[]; // {userId, username} messages: Message[]; // {messageId, from, to, message} } let dataStore: DataStore = { users: [], messages: [] }; let nextUserId = 1; let nextMessageId = 1;

Step 3: Write HTTP LayerStep 3: 写HTTP层

// server.ts - HTTP wrapper import { addUser } from './brooms'; app.post('/user', (req, res) => { const { username } = req.body; const role = req.header('role'); const result = addUser(username, role || ''); if ('error' in result) { const statusCode = getStatusCode(result.error); return res.status(statusCode).json(result); } res.json(result); });

⚙️ Q25 Type - Business Logic (e.g., Thiscord) ⚙️ Q25类型 - 业务逻辑 (如 Thiscord)

Key Strategy:关键策略: Reverse-engineer rules from test cases! Tests tell you EXACTLY what's expected. 从测试用例反向推导规则!测试告诉你精确的期望结果。

Step 1: Extract Rules from TestsStep 1: 从test反向推导规则

✗ Test 1: status='do not disturb' → false ✗ Test 2: channel in silenced → false ✓ Test 3: message contains @username → true ✓ Test 4: dm=true → true ✓ Test 5: channel in subscribed → true✗ Test 1: status='do not disturb' → false ✗ Test 2: channel在silenced里 → false ✓ Test 3: message包含@username → true ✓ Test 4: dm=true → true ✓ Test 5: channel在subscribed里 → true

Step 2: Draw Decision TreeStep 2: 画决策树

notificationService(user, message):
│
├─ 1. dm === true → return true
│
├─ 2. content contains @{username} → return true
│
├─ 3. status === 'do not disturb' → return false
│
├─ 4. channel in silenced → return false
│
├─ 5. channel in subscribed → return true
│
└─ 6. otherwise → return falsenotificationService(user, message):
│
├─ 1. dm === true → return true
│
├─ 2. content包含@{username} → return true
│
├─ 3. status === 'do not disturb' → return false
│
├─ 4. channel在silenced里 → return false
│
├─ 5. channel在subscribed里 → return true
│
└─ 6. 其他 → return false

Step 3: Write Code (Follow Decision Tree Order!)Step 3: 写代码 (按决策树顺序!)

function notificationService(user: User, message: Message): boolean { // Rule 1: DM always notify if (message.dm) return true; // Rule 2: @mention always notify if (message.content.includes(`@${user.username}`)) return true; // Rule 3: Do not disturb blocks everything else if (user.status === 'do not disturb') return false; // Rule 4: Silenced channel if (user.silenced.includes(message.channel)) return false; // Rule 5: Subscribed channel if (user.subscribed.includes(message.channel)) return true; // Default: no notification return false; }
Key Points:考点:
  • Complex condition logic复杂条件判断
  • Short-circuit logic (priority matters!)短路逻辑 (优先级!)
  • String operations: includes, indexOf字符串操作: includes, indexOf
  • Array operations: includes数组操作: includes

💡 Universal Problem-Solving Tips 💡 通用解题心法

1. Read Tests First先读test,再写代码

Tests tell you the EXACT expectations. Don't guess - read the tests!test告诉你exact的期望。不要猜,看test!

2. Normal Flow First先写正常流程

Don't consider all edge cases at the start. Get the main flow working first.不要一开始就考虑所有edge case。让主流程跑通,再补全。

3. Draw Diagrams画图/写注释

There's a reason they give you scratch paper. Draw decision trees and data flow diagrams.考试时给你草稿纸是有原因的。画决策树、数据流图。

4. Trust TypeScript类型检查是你的朋友

TypeScript errors are helping you, not annoying you. Get types right first.TypeScript错误不是烦人,是帮你的。先把类型定义对。

5. Status Code Semantics状态码必须对应语义

  • 400 = Client made a mistake客户端错了
  • 401 = Who are you?你是谁?
  • 403 = I know you, but no我知道你是谁,但你不能做
  • 500 = Server broke服务器错了

6. Time Management时间管理

  • Don't get stuck on one question不要卡在一道题上
  • Move on and come back later先做其他题,回头再做
  • Partial solutions get partial marks部分解答也有分