Flatten One Level
Arguments
arr: any[] —The input array which may contain nested arrays.
Returns
any[] — Solution idea
Because only one level is flattened, you can implement it with a single pass and no recursion while preserving order and immutability.
// Basic example
flattenOnce([1, [2, 3], [4, [5]]]) // => [1, 2, 3, 4, [5]]
// Already flat
flattenOnce([1, 2, 3]) // => [1, 2, 3]
// Mixed values
flattenOnce([[1], 2, [3, [4]], 5]) // => [1, 2, 3, [4], 5]
// Sparse array: holes are skipped, as flat(1) skips them
flattenOnce([1, , [2, , 3]]) // => [1, 2, 3]Public interview reports
These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.
- Blind: front end interview questions by company, October 2021
A candidate who interviewed at several companies lists flattening an array among the Facebook screening questions. Flattening one level is a representative drill, not the reported depth.
- Juejin: Shopee frontend interview record, two March 2020 loops
The seller-platform first round set two coding tasks: flatten an array (拍平数组) and deep-copy an object. This one-level flatten is a practice version of the first task, not its wording.
- Blind: Anduril frontend interview thread, May 2024
A commenter in a thread about the Anduril frontend interview says they cleared it and were asked lodash-style questions: flatten, map and reduce. Flattening one level is a representative drill, not the reported depth.
- interview.tw: Binance frontend engineer interview, Taiwan, 2022
In the second round of the same 2022 Binance loop the candidate flattened [[1,2,3],[2,3,4]] to one level without flat. This drill flattens one level; it is not the reported wording.
Reported in a public Meta interview account. Meta frontend interview questions
Reported in a public Shopee interview account. Shopee frontend interview questions
Reported in a public Anduril interview account. Anduril frontend interview questions
Reported in a public Binance interview account. Binance frontend interview questions
Solution
Overview
Because only one level is flattened, you can implement it with a single pass and no recursion while preserving order and immutability.
Approach 1: Iterative push (explicit and clear)
Mention that non-array values are pushed as-is and that nested arrays deeper than one level remain nested. This helps clarify expected behavior and prevents over-flattening. Both loops use forEach, which visits only the indexes an array has, so the holes of a sparse array are skipped at both levels, as flat(1) skips them; a for...of loop or spread would turn each hole into undefined.
export default function flattenOnce(arr) {
const result = [];
arr.forEach((el) => {
if (Array.isArray(el)) {
el.forEach((item) => result.push(item));
} else {
result.push(el);
}
});
return result;
}
export default function flattenOnce(arr: any[]): any[] {
const result: any[] = [];
arr.forEach((el) => {
if (Array.isArray(el)) {
el.forEach((item) => result.push(item));
} else {
result.push(el);
}
});
return result;
}
Approach 2: Using built-in .flat(1) (alternate, compact syntax)
.flat(1) (alternate, compact syntax)Idea: use the modern Array.prototype.flat() method with depth 1.
We can write: arr.flat(1).
Read it as: *flatten one layer deep and leave inner arrays untouched*.
Why use it: concise, modern, and likely optimized. In this question, treat it as an alternate or follow-up solution — the manual approach helps you understand how shallow flattening works internally.
export default function flattenOnce(arr) {
return arr.flat(1);
}
export default function flattenOnce(arr: any[]): any[] {
return arr.flat(1);
}
Notes & Pitfalls
Flattening too deeply defeats the 'one-level' requirement — avoid recursion or .flat(Infinity).Mutating the original array is not allowed — always return a new one. Non-array values should remain untouched.
Empty array → returns [].Already flat array → identical output. Mixed values like [1, [2], 3]→ flattens only first-level arrays.Sparse array like [1, , [2, , 3]]→[1, 2, 3]: holes are skipped at both levels, asflat(1)skips them.
Using Array.isArray()for type checking.Using forEach, which skips holes, for shallow unpacking.Understanding .flat(depth)as a concise modern alternative.
Common mistakes on this challenge
Function is flattening too deep or not flattening arrays consistently
This task requires one-level flattening only. When item is an array, push its direct children only ( item.forEach, which also skips holes).Do not recurse into nested grandchildren ( [4]should remain nested in sample).Keep non-array values unchanged and preserve original order.
Holes in a sparse array become undefined
flat(1)skips the holes of a sparse array, whilefor...ofand spread read each hole asundefined.Loop with forEachat both levels: it visits only the indexes an array has.Or keep an index loop and skip a missing index with i in arr.Check with [1, , [2, , 3]], which should give[1, 2, 3].
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.