Question presented to candidate: "You are grouping calendar events by the Date object representing their start day. You reach for Object.groupBy() and every event ends up in ONE group. What went wrong, and how would you fix it?"
What a strong answer should cover:
- 📌 Interview term: the real, direct answer to the prompt — verified directly:
Object.groupBy()uses its grouping key as a real OBJECT property key, which means any non-string, non-symbol key (like aDateobject, or any plain object) is genuinely coerced to a string first — and every distinct object genuinely coerces to the SAME string,"[object Object]"— silently merging every group into one. - 📌 Interview term:
Map.groupBy()— the real fix — groups into a genuineMap, which (covered in more depth in this bank's own dedicated Map-vs-object question) can use ANY value, including an object or aDateinstance, as a real, distinct key with no coercion at all — verified directly, two distinct object keys stayed genuinely separate, with correct per-key group counts. - 📌 Interview term: the real decision rule — a precise answer names the actual decision criterion: if the grouping key is (or ever COULD be) a non-string, non-symbol value — an object, a
Date, a class instance — reach forMap.groupBy(); if the key is always a plain string (a status label, a category name),Object.groupBy()'s plain-object result is simpler to consume with dot-access andJSON.stringify. - A precise answer names that this bank's own dedicated Object.groupBy-vs-Map.groupBy question covers the core ES2024 mechanism and null-prototype-result distinction in depth — this question's own value-add is the specific, real, silent-data-loss FAILURE MODE with non-string keys, and the practical "which one do I reach for" decision it drives.
- A precise answer names the fix does not require restructuring the grouping logic at all — only swapping
Object.groupBy(items, fn)forMap.groupBy(items, fn), since both share an identical callback signature.
Clarifying questions expected:
- None — this is a definitional/practical question; directly diagnosing the prompt's own described bug (verified via real reproduction) is the strong signal.
Code / implementation expected: Yes — a real, direct reproduction of the merging bug with distinct object keys, and the Map.groupBy() fix, both genuinely executed.