Skip to the solution
EasyFrontend6 min read

How would you use Object.groupBy() to group an array of orders by status, and how does it compare to a manual reduce()?

1.2k views

The problem

Question presented to candidate: "Given an array of orders, each with a status field, write code to group them by status. Do it two ways β€” with Object.groupBy() and with reduce() β€” and tell me what genuinely differs between the two results."

What a strong answer should cover:

  • πŸ“Œ Interview term: Object.groupBy(items, callback) β€” a real, built-in ES2024 method that genuinely produces the identical shape a hand-written reduce()-based groupBy has produced for years β€” an object whose keys are the callback's return values and whose values are arrays of the matching items β€” but built-in, shorter, and with one real correctness advantage over a naive reduce.
  • πŸ“Œ Interview term: the real, direct verified comparison β€” verified directly: both approaches produced the exact same real grouped JSON shape for a realistic orders-by-status array β€” confirming Object.groupBy() is a genuine, correct, drop-in shortcut for the common reduce()-based grouping pattern, not a different algorithm.
  • πŸ“Œ Interview term: the real correctness advantage β€” verified directly: a common, real reduce()-based groupBy BUG β€” forgetting to initialize the accumulator's array for a new key before pushing β€” genuinely throws a real TypeError; Object.groupBy() has no equivalent failure mode, since the built-in handles bucket-creation internally.
  • πŸ“Œ Interview term: the null-prototype difference β€” verified directly: Object.groupBy()'s result object genuinely has NO prototype at all (Object.getPrototypeOf() === null) β€” a real, deliberate safety choice β€” while a plain reduce()-built object genuinely inherits normal Object.prototype methods like hasOwnProperty.
  • A precise answer names WHY the null-prototype choice matters: it genuinely prevents a real, rare-but-real collision where an order status happened to literally be the string "hasOwnProperty" or "constructor" from silently shadowing or colliding with an inherited method on a plain reduce-built object.

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; directly implementing both approaches and comparing their real, verified output is the strong signal.

Code / implementation expected: Yes β€” both a real Object.groupBy() call and a real, equivalent reduce() implementation, executed side by side with identical input.

  • es2024
  • tricky
  • real-world

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, direct proof: Object.groupBy() and an equivalent reduce() produce byte-identical output, while a common reduce bug genuinely throws β€” verified directly

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 2 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 the difference between var, let, and const?

Continue