Skip to the solution
EasyFrontend7 min read

What does Array.filter do?

950 views

The problem

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 which predicate returns truthy β€” the real predicate receives (element, index, array).
  • πŸ“Œ Verified, not assumed β€” directly answering the prompt's mutation question: a real filter call 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 real filter β†’ map β†’ reduce chain 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 for map/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 removed 0, "", null, undefined, and NaN while 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, filter never mutates, always safe for immutability requirements.
  • "Could the actual source array genuinely contain holes (from a prior delete, or new 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.

  • filter

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: filter genuinely never mutates the original array, is chainable, and genuinely skips real holes in a sparse array

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 9 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is hoisting in JavaScript?

Continue