跳至正文

Sayli,助力求职面试

你的经验,表达清楚。

带上你的项目经历,再难的问题,也能说清思路。

产品演示使用英语。

Technical interview: Senior engineer. Daniel Novak asks: Implement an async map with a concurrency limit. Preserve input order, stop scheduling on failure, and support cancellation. Sayli: Use a shared index and a fixed pool of workers. Each worker claims its index before awaiting, so two workers cannot claim the same item.

```ts
async function mapLimit<T, R>(
  items: readonly T[],
  limit: number,
  run: (item: T, signal?: AbortSignal) => Promise<R>,
  signal?: AbortSignal,
): Promise<R[]> {
  if (!Number.isInteger(limit) || limit < 1)
    throw new RangeError('limit must be a positive integer');
  signal?.throwIfAborted();

  const results = new Array<R>(items.length);
  let next = 0;
  let failed = false;

  async function worker() {
    while (!failed) {
      signal?.throwIfAborted();
      const i = next++;
      if (i >= items.length) return;
      try {
        results[i] = await run(items[i], signal);
      } catch (error) {
        failed = true;
        throw error;
      }
    }
  }

  await Promise.all(Array.from(
    { length: Math.min(limit, items.length) }, worker,
  ));
  return results;
}
```

**Why it works:** results are written by input index, not completion order. At most `limit` calls are in flight. A rejection stops new work; already-running tasks finish unless their implementation honours the abort signal.

**Edge cases:** empty input, a limit larger than the input, out-of-order completion, a rejected task, and cancellation before or during the call. Time is O(n) bookkeeping plus the async work; result storage is O(n).
查看 Sayli 的回答

Use a shared index and a fixed pool of workers. Each worker claims its index before awaiting, so two workers cannot claim the same item.

ts
async function mapLimit<T, R>(
  items: readonly T[],
  limit: number,
  run: (item: T, signal?: AbortSignal) => Promise<R>,
  signal?: AbortSignal,
): Promise<R[]> {
  if (!Number.isInteger(limit) || limit < 1)
    throw new RangeError('limit must be a positive integer');
  signal?.throwIfAborted();

  const results = new Array<R>(items.length);
  let next = 0;
  let failed = false;

  async function worker() {
    while (!failed) {
      signal?.throwIfAborted();
      const i = next++;
      if (i >= items.length) return;
      try {
        results[i] = await run(items[i], signal);
      } catch (error) {
        failed = true;
        throw error;
      }
    }
  }

  await Promise.all(Array.from(
    { length: Math.min(limit, items.length) }, worker,
  ));
  return results;
}

Why it works: results are written by input index, not completion order. At most limit calls are in flight. A rejection stops new work; already-running tasks finish unless their implementation honours the abort signal.

Edge cases: empty input, a limit larger than the input, out-of-order completion, a rejected task, and cancellation before or during the call. Time is O(n) bookkeeping plus the async work; result storage is O(n).

下一轮面试,准备更充分。

记下问题、你的回答,以及接下来要准备的内容。

示例 1/3

决定记清楚,承诺不遗漏。

Technical interview: Senior engineer. Daniel asked for an async map with a concurrency limit. You discussed keeping results in input order, stopping new work on failure and passing cancellation to each operation. Next steps: Practise the failure and cancellation walkthrough, You, Thu. Prepare tests for out-of-order results, You, Thu.

通话示例 · Technical interview: Senior engineer

一天中的其他通话,也有帮助。

下一轮,展现更好的自己。

免费开始。需要时,你的背景信息就在手边。