CodingNeed.

Engineering practice · 25 MIN

Keep generic grouping type safe

Preserve the relationship between records and keys.

A generic helper should retain the input type in its result. Map avoids prototype-key collisions such as __proto__ and keeps first-seen key order. When serializing a Map to JSON, convert it to an explicit entry array. Structural types describe shape; they do not enforce business authorization.

Treat the function as a small service: define a contract, maintain an invariant, and test the boundaries.

Read the example

const groups = new Map<string, number[]>();
groups.set("__proto__", [1]);
console.log(groups.get("__proto__")?.length);
Check the expected output
1

Your challenge

Input is an array of {team:string,points:number}. Return [[team,total],...] in first-seen team order. Teams can be any string, including __proto__. Do not mutate the input.

Solution cost: Expected O(n) time · O(n + k) for grouped rows and k returned totals. space

Common trap

A plain object used as a map may collide with inherited properties.

Further reading: TypeScript: generics

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.