JavaScript16 min read

JavaScript Array Interview Questions: 15 Coding Problems

Practice 15 essential JavaScript array coding problems including Two Sum, flatten, deduplication, chunking, and custom array utilities with optimal time complexity.

Pairlet TeamPublished: 2026-09-08

Arrays are the primary data structure tested in JavaScript coding interviews. Mastering array manipulation methods (map, filter, reduce, slice, splice) and hash map optimization strategies enables candidates to solve complex data processing tasks efficiently.

1. Two Sum Problem (O(N) Time Complexity)

Given an array of numbers and a target sum, return the indices of the two numbers that add up to the target.

javascriptPairlet Snippet
function twoSum(nums, target) {
  const map = new Map();
  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i];
    if (map.has(complement)) {
      return [map.get(complement), i];
    }
    map.set(nums[i], i);
  }
  return [];
}
```

2. Flatten a Nested Array

Implement a function to recursively flatten an array up to a specified depth.

javascriptPairlet Snippet
function flatten(arr, depth = 1) {
  if (depth <= 0) return arr.slice();
  return arr.reduce((acc, val) => {
    if (Array.isArray(val)) {
      acc.push(...flatten(val, depth - 1));
    } else {
      acc.push(val);
    }
    return acc;
  }, []);
}
```

3. Array Deduplication (Unique Values)

javascriptPairlet Snippet
// Primitive values

// Object values by property key function uniqueBy(arr, key) { const seen = new Set(); return arr.filter(item => { const val = item[key]; if (seen.has(val)) return false; seen.add(val); return true; }); } ```

4. Group Array Elements by Key (groupBy)

javascriptPairlet Snippet
function groupBy(array, keyFn) {
  return array.reduce((result, item) => {
    const key = typeof keyFn === "function" ? keyFn(item) : item[keyFn];
    if (!result[key]) {
      result[key] = [];
    }
    result[key].push(item);
    return result;
  }, {});
}
```

5. Chunk an Array into Sub-Arrays

javascriptPairlet Snippet
function chunk(array, size) {
  const chunked = [];
  for (let i = 0; i < array.length; i += size) {
    chunked.push(array.slice(i, i + size));
  }
  return chunked;
}

---

Run Array Coding Tasks Live Conduct live technical interviews with instant code execution. [Create a free Pairlet interview room](https://www.pairlet.dev/interview/new).

Frequently Asked Questions

How do you optimize array lookup from O(N) to O(1)?

By creating a hash map (object or Map) to store elements or frequency counts as keys, allowing constant time O(1) lookups during iteration.

Practice Relevant Coding Problems
Practice Live Coding

Conduct Live Coding Interviews with Zero Friction

No candidate sign-up required. Create an instant room, share the link, and code together in real time with shared code execution.

Related Articles