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