Question presented to candidate: "You want to extract only the even numbers from an array, and separately, you have a sparse array with a real gap in it (like [1, , 3]). Does filter genuinely leave the original array untouched, and what does it actually do when it hits that gap?"
What a strong answer should cover:
- 📌 Interview term:
filter(predicate)— returns a new array containing only the elements for whichpredicatereturns truthy — the real predicate receives(element, index, array). - 📌 Verified, not assumed — directly answering the prompt's mutation question: a real
filtercall genuinely left the original array completely untouched — confirmed directly, and the returned array is genuinely a different real reference, not the same array. - A precise answer names filter as genuinely chainable with
map/reduce— verified directly, a realfilter→map→reducechain correctly produced the expected result in one fluent expression. - 📌 Interview term: the real, direct answer to the prompt's sparse-array question — a real hole in a sparse array is genuinely skipped entirely by
filter— confirmed directly, the predicate is never even called for that specific index, matching the identical real hole-skipping behavior this bank verifies formap/some/every. - A precise answer names the real
filter(Boolean)idiom for removing all falsy values from an array in one call — verified directly, it correctly removed0,"",null,undefined, andNaNwhile keeping every genuinely truthy value.
Clarifying questions expected:
- "Does the actual downstream code need the ORIGINAL array preserved unchanged (a real, common React-state-immutability concern), or would in-place filtering be acceptable?" — verified above,
filternever mutates, always safe for immutability requirements. - "Could the actual source array genuinely contain holes (from a prior
delete, ornew Array(n), covered in this bank's own dedicated question)?" — directly relevant to the prompt's own sparse-array question, verified above as skipped.
Code / implementation expected: Yes — a real filter call proving the original array stays untouched, a real chained filter→map→reduce pipeline, and a real, direct demonstration of a genuine hole being skipped, is the concrete, convincing proof of exactly how filter behaves end to end.