JavaScript1 min read

JavaScript Array Interview Questions and Practice Problems

Practice essential JavaScript array coding problems, including Two Sum, flattening, deduplication, chunking, and custom array utilities.

Pairlet Team•Published: •Updated:

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.

JAVASCRIPT
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 [];
}

*Try Two Sum live on Pairlet.*

2. Flatten a Nested Array

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

JAVASCRIPT
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;
  }, []);
}

*Practice Flatten Array on Pairlet.*

3. Array Deduplication (Unique Values)

JAVASCRIPT
// Primitive values
const uniqueNumbers = (arr) => [...new Set(arr)];

// 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)

JAVASCRIPT
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;
  }, {});
}

*Try Group By on Pairlet.*

5. Chunk an Array into Sub-Arrays

JAVASCRIPT
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 real-time code execution. Create a Pairlet interview room.

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 in the Browser

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

Related Articles