Easy Interview Questions
These are the warm-up array questions you will meet in almost every JavaScript interview. Try each one yourself before opening the solution. Interviewers care less about the one-liner and more about whether you can explain the cost and the edge cases (empty array, duplicates, negative numbers).
All solutions are in practice.js, with checks that prove they work. Run it with node practice.js.
1. Reverse an array
Reverse an array without using .reverse().
reverseArray([1, 2, 3, 4]); // [4, 3, 2, 1]
Solution
function reverseArray(arr) {
const out = [];
for (let i = arr.length - 1; i >= 0; i--) out.push(arr[i]);
return out;
}
Time: O(n) · Space: O(n)
Follow-up: "Do it in place" - swap arr[i] and arr[j] with two pointers moving toward the middle (O(1) space). Remember .reverse() itself mutates the array.
2. Find the max and min
Return the largest and smallest number in one pass.
maxMin([3, -1, 9, 4]); // { max: 9, min: -1 }
Solution
function maxMin(arr) {
let max = -Infinity;
let min = Infinity;
for (const n of arr) {
if (n > max) max = n;
if (n < min) min = n;
}
return { max, min };
}
Time: O(n) · Space: O(1)
Gotcha: Math.max(...arr) is shorter but throws a RangeError on very large arrays (too many arguments), and Math.max() of an empty array is -Infinity.
3. Sum of an array
sum([1, 2, 3, 4]); // 10
Solution
const sum = (arr) => arr.reduce((acc, n) => acc + n, 0);
Time: O(n) · Space: O(1)
Gotcha: always pass the initial value 0. Without it, [].reduce(...) throws a TypeError.
4. Remove duplicates
removeDuplicates([1, 2, 2, 3, 1]); // [1, 2, 3]
Solution
const removeDuplicates = (arr) => [...new Set(arr)];
Time: O(n) · Space: O(n)
Follow-up: "Without a Set" - arr.filter((x, i) => arr.indexOf(x) === i) works but is O(n²) and drops NaN. For objects, dedupe by a key with a Map (see 07-real-world-usecases/01-deduplication).
5. Is the array a palindrome?
Return true if the array reads the same forwards and backwards.
isPalindrome([1, 2, 3, 2, 1]); // true
isPalindrome([1, 2, 3]); // false
Solution
function isPalindrome(arr) {
for (let i = 0, j = arr.length - 1; i < j; i++, j--) {
if (arr[i] !== arr[j]) return false;
}
return true;
}
Time: O(n) · Space: O(1)
Gotcha: arr.join() === [...arr].reverse().join() looks clever but breaks for elements that contain commas and uses O(n) extra space.
6. Count occurrences
Count how many times each value appears.
countOccurrences(["a", "b", "a", "c", "a"]); // { a: 3, b: 1, c: 1 }
Solution
function countOccurrences(arr) {
const counts = {};
for (const x of arr) counts[x] = (counts[x] || 0) + 1;
return counts;
}
Time: O(n) · Space: O(k) for k distinct values
Gotcha: object keys are strings, so 1 and "1" are counted together. Use a Map if the type matters.
7. Flatten one level
flattenOnce([1, [2, 3], [4, [5]]]); // [1, 2, 3, 4, [5]]
Solution
const flattenOnce = (arr) => [].concat(...arr);
// or simply: arr.flat()
Time: O(n) · Space: O(n)
Follow-up: "Flatten every level" - that is a medium question (recursion or a stack), or just arr.flat(Infinity).
8. Second largest number
Return the second largest distinct value, or null if there is none.
secondLargest([10, 5, 10, 8]); // 8
secondLargest([7, 7]); // null
Solution
function secondLargest(arr) {
let first = -Infinity;
let second = -Infinity;
for (const n of arr) {
if (n > first) {
second = first;
first = n;
} else if (n > second && n < first) {
second = n;
}
}
return second === -Infinity ? null : second;
}
Time: O(n) · Space: O(1)
Gotcha: sorting and taking arr[arr.length - 2] is O(n log n) and returns 10 for [10, 5, 10, 8] because of the duplicate.
9. Chunk an array
Split an array into groups of size.
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
Solution
function chunk(arr, size) {
const out = [];
for (let i = 0; i < arr.length; i += size) out.push(arr.slice(i, i + size));
return out;
}
Time: O(n) · Space: O(n)
Gotcha: guard against size <= 0, otherwise the loop never ends.
10. Merge two sorted arrays
Merge two sorted arrays into one sorted array.
mergeSorted([1, 4, 7], [2, 3, 8, 9]); // [1, 2, 3, 4, 7, 8, 9]
Solution
function mergeSorted(a, b) {
const out = [];
let i = 0;
let j = 0;
while (i < a.length && j < b.length) out.push(a[i] <= b[j] ? a[i++] : b[j++]);
return out.concat(a.slice(i), b.slice(j));
}
Time: O(n + m) · Space: O(n + m)
Follow-up: [...a, ...b].sort((x, y) => x - y) works but is O((n + m) log(n + m)) and ignores that the inputs are already sorted, which is the whole point of the question.
11. Rotate an array by k
Rotate the array to the right by k steps.
rotateRight([1, 2, 3, 4, 5], 2); // [4, 5, 1, 2, 3]
Solution
function rotateRight(arr, k) {
if (arr.length === 0) return [];
k %= arr.length;
return [...arr.slice(-k), ...arr.slice(0, -k)];
}
Time: O(n) · Space: O(n)
Gotcha: always do k %= arr.length, since k can be larger than the array. The in-place O(1) space version (three reverses) is in the medium section.
12. Find the missing number
An array holds the numbers 1..n with exactly one missing. Find it.
missingNumber([1, 2, 4, 5, 6]); // 3
Solution
function missingNumber(arr) {
const n = arr.length + 1;
const expected = (n * (n + 1)) / 2;
return expected - arr.reduce((a, b) => a + b, 0);
}
Time: O(n) · Space: O(1)
Follow-up: for huge n the sum can lose precision; XOR-ing all indices and values avoids overflow.
13. Predict the output: coercion and sort
What does each line print?
[1, 2, 3] + [4];
[10, 1, 2].sort();
["1", "2", "3"].map(parseInt);
Solution
[1, 2, 3] + [4]; // "1,2,34"
[10, 1, 2].sort(); // [1, 10, 2]
["1", "2", "3"].map(parseInt); // [1, NaN, NaN]
+turns both arrays into strings ("1,2,3"and"4") and concatenates them.sort()with no comparator compares elements as strings, and"10" < "2". Usesort((a, b) => a - b)for numbers.mappasses(value, index), so the calls areparseInt("1", 0),parseInt("2", 1),parseInt("3", 2). Radix 1 is invalid and"3"is not a binary digit. Use.map(Number).
Gotcha: sort() also mutates the original array.
14. Predict the output: length and sparse arrays
const a = [1, 2, 3];
a.length = 1;
console.log(a);
const b = [];
b[5] = "x";
console.log(b.length, b[0], 0 in b);
console.log([, ,].length);
Solution
[1] // setting length smaller deletes elements
6 undefined false // index 5 makes length 6; indexes 0-4 are holes, not undefined values
2 // a trailing comma is ignored, so [, ,] has 2 holes
Gotcha: holes are skipped by forEach, map, filter and reduce, but for...of and spread treat them as undefined.
Interview tip
For every easy question, say the brute-force idea in one sentence, give the O(n) version, then mention one edge case (empty array, duplicates, very large input). That shows you think beyond the happy path.