Question presented to candidate: "If you need to group a list of items by their category, and each category is represented by an OBJECT (not a string), which of Object.groupBy or Map.groupBy would actually work correctly, and which would silently produce a wrong result?"
What a strong answer should cover:
- 📌 Interview term:
Object.groupBy(items, keyFn)— returns a plain, null-prototype object (covered in more depth in this bank's own dedicated question) — its keys are genuinely coerced to strings, exactly like any plain object property key. - 📌 Interview term:
Map.groupBy(items, keyFn)— returns a realMapinstance instead — its keys are genuinely not coerced at all, accepting any value, including an object, as a real, distinct key. - 📌 Interview term: the real, direct answer to the prompt — verified directly: grouping by an object category key with
Object.groupBygenuinely coerces EVERY object key to the identical literal string"[object Object]", silently merging what should have been separate groups into one — a real, silent data-corruption bug.Map.groupByon the identical input genuinely keeps the object key as a real, distinct key, correctly preserving separate groups. - 📌 Interview term: return type consequences — a precise answer names the practical downstream differences:
Object.groupBy's result works withObject.keys()/for...in/dot-or-bracket access, whileMap.groupBy's result requires.get()/.has()/for...of— the identical real API differences this bank's own Map/Set-vs-objects/arrays question covers generally, just applied specifically to the two grouping functions. - A precise answer names that both genuinely share the identical grouping LOGIC and callback signature — the only real differences are the coercion behavior and the resulting container type, not the grouping algorithm itself.
Clarifying questions expected:
- None — this is a definitional/comparison question; directly answering the prompt's own object-category scenario (naming which one silently breaks) is the strong signal.
Code / implementation expected: Yes — the real, side-by-side object-key grouping test — showing Object.groupBy silently merging groups while Map.groupBy correctly keeps them separate — is the clearest, most convincing demonstration.