Problem, as an interviewer would state it:
"Implement indexBy(array, key) — converting an array into an object keyed by a property (or by a function's result) for O(1) lookup, with sensible handling for duplicate keys and for hostile keys such as __proto__."
Examples:
indexBy([{ id: 1, n: "a" }, { id: 2, n: "b" }], "id");
// { 1: { id: 1, n: "a" }, 2: { id: 2, n: "b" } }
Clarifying questions expected:
- When two items produce the same key, should the first or last win — or should it be an error?
- Is the key property guaranteed to be safe, or could it come from untrusted data?
- Should a function be accepted as well as a property name?
Code / implementation expected: Yes — real, direct proof of each collision mode and of the prototype-safety behavior.