Skip to the solution
MediumFrontend6 min read

How does Array.reduce work?

822 views

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

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

6 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 reduce: grouping items into an object, plus the real empty-array-with-vs-without-initial-value contrast

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 63 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 nextMedium

What is event delegation?

Continue