Javascript · Arrays

Arrays Interview Questions
With Answers & Code Examples

4 carefully curated Arrays interview questions with working code examples and real interview gotchas.

Practice Interactively →← All Categories
4 questions0 beginner4 core0 advanced
Q1Core

When would you use map vs forEach vs reduce vs filter?

💡 Hint: map=transform, filter=subset, reduce=accumulate, forEach=side effects
  • map — transform each element, returns new array of same length
  • filter — keep matching elements, returns smaller array
  • reduce — accumulate into single value (any type)
  • forEach — side effects only, returns undefined, not chainable
const nums = [1,2,3,4,5];
nums.map(n => n * 2);                // [2,4,6,8,10]
nums.filter(n => n % 2 === 0);       // [2,4]
nums.reduce((sum, n) => sum + n, 0); // 15

// Chaining
nums.filter(n => n > 2).map(n => n ** 2); // [9,16,25]
Practice this question →
Q2Core

How do Array.find(), findIndex(), some(), and every() work?

💡 Hint: find=first match value, findIndex=first match index, some=any passes, every=all pass
  • find() — returns the first element where callback returns true, or undefined
  • findIndex() — returns the index of first match, or -1
  • some() — returns true if at least one element passes (short-circuits)
  • every() — returns true only if ALL elements pass (short-circuits)
const users = [
  { id: 1, name: 'Alice', active: true  },
  { id: 2, name: 'Bob',   active: false },
  { id: 3, name: 'Carol', active: true  },
];

users.find(u => u.id === 2);       // { id: 2, name: 'Bob', active: false }
users.findIndex(u => u.id === 2);  // 1
users.find(u => u.id === 99);      // undefined (not found)

users.some(u => u.active);  // true  (stops at Alice)
users.every(u => u.active); // false (stops at Bob)

// Short-circuit saves work
users.some(u => {
  console.log('checking', u.name);
  return u.name === 'Alice'; // only checks Alice — stops immediately
});
💡 some() is like logical OR across the array; every() is like AND. Use find() when you need the value, findIndex() when you need the position.
Practice this question →
Q3Core

How do Array.flat() and Array.flatMap() work?

💡 Hint: flat() flattens nested arrays; flatMap() maps then flattens one level — more efficient
const nested = [1, [2, [3, [4]]]];

nested.flat();         // [1, 2, [3, [4]]] — default depth 1
nested.flat(2);        // [1, 2, 3, [4]]
nested.flat(Infinity); // [1, 2, 3, 4] — fully flat

// flatMap = map + flat(1) — more efficient than separate calls
const sentences = ['Hello World', 'Foo Bar'];
sentences.flatMap(s => s.split(' ')); // ['Hello', 'World', 'Foo', 'Bar']

// vs two-step (less efficient)
sentences.map(s => s.split(' ')).flat(); // same result

// flatMap can filter + transform in one pass
const nums = [1, 2, 3, 4, 5];
nums.flatMap(n => n % 2 === 0 ? [n, n * 10] : []);
// [2, 20, 4, 40] — odds removed, evens doubled
// Return [] to skip, [val] to keep, [a, b] to expand one item into two
💡 flatMap is more efficient than map+flat because it only iterates once. Use it as a combined filter+map by returning [] to skip items.
Practice this question →
Q4Core

What are immutable array operations and the new ES2023 methods?

💡 Hint: toSorted, toReversed, toSpliced, with — immutable versions of mutating methods

Many array methods mutate the original. Always be aware of which do and which don't:

Mutating (change original): sort, reverse, splice, push, pop, shift, unshift, fill

Non-mutating (return new): map, filter, slice, concat, flat, flatMap, reduce

const arr = [3, 1, 2];

// Old pattern — copy first to avoid mutation
const sorted = [...arr].sort((a, b) => a - b); // arr unchanged

// ES2023 — built-in immutable versions
arr.toSorted((a, b) => a - b);  // [1, 2, 3] — arr still [3, 1, 2]
arr.toReversed();               // [2, 1, 3] — arr still [3, 1, 2]
arr.toSpliced(1, 1, 9);         // [3, 9, 2] — arr still [3, 1, 2]
arr.with(1, 99);                // [3, 99, 2] — replace index 1

// All 4 return NEW arrays without touching the original
console.log(arr); // [3, 1, 2] ✓
💡 Immutable operations are essential in React/Redux where you must not mutate state. Prefer [...arr].sort() or arr.toSorted() over arr.sort().
Practice this question →

Other Javascript Interview Topics

Rendering StrategiesCore JSType SystemReact FundamentalsFunctionsMicrofrontendsGenericsAsync JSHooksObjectsMonorepo'this' KeywordUtility TypesError HandlingModern JSBundle OptimizationPerformanceDOM & EventsState ManagementClasses & OOPCaching StrategiesComponent PatternsAdvanced TypesAuthenticationReact RouterFormsAdvanced PatternsFrontend SecurityConcurrent ReactServer ComponentsTestingEcosystemNetwork OptimizationCore Web VitalsBrowser APIs

Ready to practice Arrays?

Get AI feedback on your answers, predict code output, and fix real bugs.

Start Free Practice →