Problem, as an interviewer would state it:
"Implement Array.prototype.myFlatMap from scratch — mapping each element and then flattening the result by exactly ONE level, matching real native flatMap's own fixed-depth contract."
Examples:
[1, 2, 3].flatMap(n => [n, n * 2]); // [1, 2, 2, 4, 3, 6]
Clarifying questions expected:
- Is the flattening depth for flatMap genuinely fixed at exactly 1 level, with no way to configure it deeper?
- If the callback returns a non-array value for some elements, is that value simply pushed directly, without error?
- Is flatMap genuinely equivalent to calling map() then flat(1) separately, or is there a real, meaningful difference?
Code / implementation expected: Yes — real, direct proof against the ACTUAL native flatMap, including a case with a doubly-nested result, confirming the ONE-level-only flattening behavior exactly.