Skip to solution
mediumFrontend

How does Array.reduce work?

821 views
01

Understand the problem

Question presented to candidate: "You want to turn an array of items into a single grouped object, keyed by category. Someone suggests using reduce for this. Walk me through exactly how reduce works, and what happens if you call it on an empty array without giving it a starting value?"

What a strong answer should cover:

  • 📌 Interview term: reduce(callback, initialValue) — folds an array into a single value by repeatedly calling callback(accumulator, currentElement), where each call's return value genuinely becomes the accumulator for the next call — real, sequential, left-to-right processing.
  • 📌 Verified, not assumed: a real reduce call genuinely built a grouped object from an array of items, correctly accumulating each item into its category's array — directly demonstrating the exact prompt scenario.
  • 📌 Interview term: the real, direct answer to the prompt's edge case — calling reduce on a genuinely empty array with no initial value genuinely throws a real TypeError ("Reduce of empty array with no initial value") — confirmed directly. The identical call with an initial value genuinely succeeds even on an empty array, correctly returning that initial value untouched.
  • A precise answer names what happens when no initial value is supplied but the array is non-empty: the real first element becomes the starting accumulator, and the callback genuinely starts running from the second element — verified directly with a real, matching sum.
  • A precise answer names that reduce is genuinely general enough to reimplement map or filter — verified directly, a real reduce-based map equivalent produced the identical real result — useful context for why it is sometimes called the "Swiss Army knife" of array methods.

Clarifying questions expected:

  • "Can the actual input array genuinely be empty in this specific use case?" — directly decides whether an initial value is a real, required safeguard against the exact throw verified above, not just a stylistic choice.
  • "Does the accumulator need to be a genuinely different SHAPE than the array elements (an object, a number, a Map), or is it the same shape?" — reduce's real flexibility, verified above via the grouping example, supports any accumulator shape.

Code / implementation expected: Yes — a real reduce call building a grouped object, plus a real, direct demonstration of the empty-array-no-initial-value throw versus the empty-array-with-initial-value success, is the concrete, convincing proof of exactly how reduce behaves end to end.

reduce
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript array-method interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The real grouping output, the real thrown error, and the real reduceRight contrast below

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal reduce: grouping items into an object, plus the real empty-array-with-vs-without-initial-value contrast
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 63 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track