DrillLab
练习Practice

动手做Get your hands on it

练习跟着课文走 —— 每节课尾都有本课的练习。这一页是全部练习的总库,想集中刷题的时候来。 每个练习都写清了它来自哪一节,卡住了就回去看那一节。Practice follows the lessons — every lesson ends with the exercises for that lesson. This page is the whole library, for when you want to drill in one sitting. Each exercise names the lesson it came from, so you can go back when you stall.

0 / 148个做对过you got right

已筛到你正在学的《面试八股》。想看全部就点上面的「全部」。Filtered to Interview questions — the course you are on. Use “All” above to see everything.

练习Exercises

筛出 16 个练习(共 148 个) · 第 2 / 2 页。Showing 16 of 148 · page 2 / 2.
来自From 异步与结构:Promise.all、EventEmitter、LRUAsync and structure: Promise.all, EventEmitter, LRU · 面试八股Interview questions
L3写整块Write a block手写 EventEmitterWrite an EventEmitter by hand
把空骨架填成完整的 EventEmitter:on / off / once / emit。 重点:once 触发时不能挤掉同一事件的其他监听器。Fill the empty skeleton in to a complete EventEmitter: on, off, once and emit. The point to watch: when a once listener fires, it must not push aside the other listeners on the same event.
要求Requirements
  • on 注册;emit 按注册顺序调用所有监听器并传参on registers a listener; emit calls every listener in registration order and passes the arguments along
  • off 只移除指定的那一个监听器off removes only the one listener it was given
  • once 只触发一次,且不挤掉同一事件的其他监听器once fires exactly once, and does not push aside the other listeners on the same event
  • emit 返回「有没有人在听」emit returns whether anyone was listening
TypeScriptemitter.ts
This check is textual: it looks for the right constructs, it does not run your code
提示Hints共 4 级,已看 0 级4 levels, 0 opened
先自己想两分钟。想不出来再点右上角 —— 提示是一级一级放的,不会一次给完。Think for two minutes first. Then use the button above — hints come one level at a time, never all at once.

看答案之前,先确认你已经自己动手写过一遍。看懂别人的答案和自己写出来,是两种能力。Before you open this, make sure you have written it yourself once. Following someone else's answer and producing your own are two different skills.

来自From 异步与结构:Promise.all、EventEmitter、LRUAsync and structure: Promise.all, EventEmitter, LRU · 面试八股Interview questions
L3写整块Write a block手写 LRUCache(用 Map,不写链表)Write an LRUCache by hand (use a Map, no linked list)
把「什么都没存」的骨架写成真正的 LRU:超容量淘汰最久未使用, get 和 put 都要刷新「最近用过」。This skeleton stores nothing. Turn it into a real LRU: over capacity, drop the entry that has gone unused the longest, and both get and put must mark an entry as recently used.
要求Requirements
  • get / put 基本读写;get 不到返回 undefinedBasic reads and writes through get and put; a miss on get returns undefined
  • 超容量时淘汰最久未使用的那条Over capacity, drop the entry that has gone unused the longest
  • get 命中要刷新「最近用过」A hit on get marks that entry as recently used
  • put 已存在的 key:更新值并刷新put on a key that already exists updates the value and marks it as recently used
  • capacity 为 1 也要正确A capacity of 1 still behaves correctly
TypeScriptlru.ts
This check is textual: it looks for the right constructs, it does not run your code
提示Hints共 4 级,已看 0 级4 levels, 0 opened
先自己想两分钟。想不出来再点右上角 —— 提示是一级一级放的,不会一次给完。Think for two minutes first. Then use the button above — hints come one level at a time, never all at once.

看答案之前,先确认你已经自己动手写过一遍。看懂别人的答案和自己写出来,是两种能力。Before you open this, make sure you have written it yourself once. Following someone else's answer and producing your own are two different skills.

来自From Utility Types:会用,还要会手写Utility types: use them, and write them yourself · 面试八股Interview questions
L1认出来Spot it认出这个 mapped type 在干什么Work out what this mapped type doesDrillLab 自出Written by DrillLab

面试官给出下面这个类型,问它对 T 做了什么。

An interviewer shows you the type below and asks what it does to T.

TypeScriptMystery.ts示意Illustrative
1type Mystery<T> = {
2 [K in keyof T]-?: T[K];
3};
先选一个选项Pick an option first
来自From 泛型与收窄:把 any 赶出代码Generics and narrowing: getting any out of the code · 面试八股Interview questions
L1认出来Spot itunknown 参数该怎么用起来How to actually use an unknown parameterDrillLab 自出Written by DrillLab

这个函数编译不过:'e' is of type 'unknown'.下面哪种改法是对的?

This function does not compile: 'e' is of type 'unknown'. Which fix is the right one?

TypeScriptreport.ts示意Illustrative
1function report(e: unknown) {
2 console.log(e.message);
3 // ^ ✗ 'e' is of type 'unknown'.
4}
先选一个选项Pick an option first