Problem, as an interviewer would state it:
"Write an asyncFilter(items, predicate) where predicate returns a Promise<boolean>. It should behave like Array.prototype.filter, but await each async check. Show me the difference between doing this the naive way and the fast way."
Examples:
const isEvenAsync = async (n) => { await delay(); return n % 2 === 0; };
await asyncFilter([1,2,3,4,5,6], isEvenAsync); // [2, 4, 6]
Clarifying questions expected:
- Do the async checks need to run in a specific order relative to each other, or is running them concurrently fine?
- Should a rejected predicate call fail the whole operation, or be treated as
false? - Is there a concurrency limit needed, or can every check run at once regardless of
items.length?
Code / implementation expected: Yes — real, direct proof that a sequential and a parallel implementation produce identical results, with the parallel version measurably faster on real, independently-delayed async checks.