Question presented to candidate: "You have an array of 1000 users, and you want to check if any of them are under 18. Would some() actually stop checking once it finds one, or does it genuinely check all 1000 regardless? Same question for every() — checking that all users are adults."
What a strong answer should cover:
- 📌 Interview term:
some(predicate)— returnstrueif at least one element genuinely passes the predicate;📌 every(predicate)** — returnstrueonly if all elements genuinely pass. - 📌 Verified, not assumed — directly answering the prompt's exact question: a real call counter proved both methods genuinely short-circuit —
some()genuinely stopped calling its predicate at exactly the element that first returnedtrue(not continuing through the rest of the array), andevery()genuinely stopped at exactly the element that first returnedfalse. - 📌 Interview term: the real, vacuous-truth edge cases — confirmed directly: an empty array's
some()genuinely returnsfalse(no element exists to pass), while an empty array'severy()genuinely returnstrue(there is no element to fail the check) — a real, easy-to-get-backwards pair of facts, worth memorizing precisely rather than guessing. - A precise answer names
some/everyas answering genuinely existential ("does at least one exist") versus universal ("do all satisfy") questions about an array — the real, direct vocabulary an interviewer expects. - The precise, honest scope: both methods genuinely only report a boolean — if the actual, specific matching element itself is needed (not just whether one exists),
find()(covered in this bank's own dedicated question) is the correct, more direct tool.
Clarifying questions expected:
- "Does the actual downstream code need the specific matching element itself, or just a yes/no answer to whether one exists?" — directly decides between
some/every(verified above as boolean-only) andfind(covered in this bank's own dedicated question). - "Is the actual predicate function genuinely expensive to run per element?" — the real short-circuiting verified above is specifically valuable when each individual check has a real, meaningful cost.
Code / implementation expected: Yes — a real call counter directly proving both methods genuinely stop early, plus the real empty-array vacuous-truth outcomes, is the concrete, convincing proof of exactly how efficiently — and correctly — these two methods behave.