Review a JavaScript flatten PR
Live PR Review: JavaScript flatten Utility
You are tasked with reviewing a pull request that introduces a flatten utility function in JavaScript.
Intended Behavior
Implement flatten(input) with the following specifications:
inputis an Array that may contain nested Arrays to an arbitrary depth.- The function must return a new flat Array.
- The order of elements must be preserved from left-to-right.
Examples:
flatten([1, [2, 3], [[4]], 5])should return[1, 2, 3, 4, 5]flatten([])should return[]flatten([1, [2, [3]]])should return[1, 2, 3]
PR Code Under Review
// PR version
export function flatten(arr) {
const out = [];
function helper(x) {
for (const i in x) {
const v = x[i];
if (typeof v === 'object') {
helper(v);
} else {
out.push(v);
}
}
}
helper(arr);
return out;
}
Tasks
- Identify correctness bugs and edge cases that this implementation fails to handle.
- Discuss API/contract questions you would clarify with the author (e.g., how to treat non-array objects,
null, sparse arrays, strings). - Suggest improvements for:
- Readability and maintainability
- Performance (time and space complexity)
- Safety (e.g., stack overflow risk)
- Propose a revised approach (pseudocode or description is acceptable) and outline a minimal set of tests you would request before approving the pull request.
AI interview prep
Core dilemma
Balancing correctness and robustness across diverse input types (including non-array objects, null, sparse arrays) and arbitrary nesting depths, with performance and memory efficiency, while mitigating common JavaScript pitfalls like `for...in` and recursion limits.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Review a JavaScript flatten PR
Live PR Review: JavaScript flatten Utility
You are tasked with reviewing a pull request that introduces a flatten utility function in JavaScript.
Intended Behavior
Implement flatten(input) with the following specifications:
inputis an Array that may contain nested Arrays to an arbitrary depth.- The function must return a new flat Array.
- The order of elements must be preserved from left-to-right.
Examples:
flatten([1, [2, 3], [[4]], 5])should return[1, 2, 3, 4, 5]flatten([])should return[]flatten([1, [2, [3]]])should return[1, 2, 3]
PR Code Under Review
// PR version
export function flatten(arr) {
const out = [];
function helper(x) {
for (const i in x) {
const v = x[i];
if (typeof v === 'object') {
helper(v);
} else {
out.push(v);
}
}
}
helper(arr);
return out;
}
Tasks
- Identify correctness bugs and edge cases that this implementation fails to handle.
- Discuss API/contract questions you would clarify with the author (e.g., how to treat non-array objects,
null, sparse arrays, strings). - Suggest improvements for:
- Readability and maintainability
- Performance (time and space complexity)
- Safety (e.g., stack overflow risk)
- Propose a revised approach (pseudocode or description is acceptable) and outline a minimal set of tests you would request before approving the pull request.