DEV Community

Cover image for 5 JavaScript coding interview questions - Part 8
Saqib Jamil
Saqib Jamil

Posted on

5 JavaScript coding interview questions - Part 8

Q1: Find the Intersection of Two Arrays.
Write a function that returns the common elements between two arrays.
Ans:

function arrayIntersection(arr1, arr2) {
  return arr1.filter(item => arr2.includes(item));
}

console.log(arrayIntersection([1, 2, 3, 4], [3, 4, 5, 6])); 
// Output: [3, 4]

Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Tip: Use Set for better performance with large datasets.

Q2: Capitalize the First Letter of Each Word.
Write a function that takes a sentence and capitalizes the first letter of each word.

Ans:

function capitalizeWords(sentence) {
  return sentence
    .split(" ")
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join(" ");
}

console.log(capitalizeWords("hello world from javascript"));
// Output: "Hello World From Javascript"

Enter fullscreen mode Exit fullscreen mode

Q3: Count the Frequency of Each Element in an Array
Write a function that counts how many times each element appears in an array.

Ans:

function countFrequency(arr) {
  return arr.reduce((acc, item) => {
    acc[item] = (acc[item] || 0) + 1;
    return acc;
  }, {});
}

console.log(countFrequency(["apple", "banana", "apple", "orange", "banana", "apple"]));
// Output: { apple: 3, banana: 2, orange: 1 }

Enter fullscreen mode Exit fullscreen mode

Q4: Check if Two Strings Are Anagrams
Write a function to check whether two strings are anagrams of each other.

Ans:
Anagrams are words or phrases that can be rearranged to make an entirely different word or phrase.

function isAnagram(str1, str2) {
  const normalize = str => str.toLowerCase().split("").sort().join("");
  return normalize(str1) === normalize(str2);
}

console.log(isAnagram("listen", "silent")); // true
console.log(isAnagram("hello", "world"));   // false

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Explanation:
We normalize both strings by converting them to lowercase, sorting, and comparing.
If both sorted versions match โ€” theyโ€™re anagrams!

Q5: Chunk an Array
Write a function that splits an array into chunks of a specified size.
Ans:

function chunkArray(arr, size) {
  let result = [];
  for (let i = 0; i < arr.length; i += size) {
    result.push(arr.slice(i, i + size));
  }
  return result;
}

console.log(chunkArray([1, 2, 3, 4, 5, 6, 7], 3));
// Output: [[1, 2, 3], [4, 5, 6], [7]]

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Explanation:
This is the core part of the loop that allows the function to chunk the array efficiently. Letโ€™s go step by step ๐Ÿ‘‡
for (let i = 0; i < arr.length; i += size)

๐Ÿงฉ 1. Normal for-loop structure
A typical for loop looks like this:
for (initialization; condition; increment) {
// code to execute
}

Here:

  • initialization โ€” sets up a variable (like let i = 0)
  • condition โ€” loop continues while this is true
  • increment โ€” runs after each loop iteration (to move to the next step)

๐Ÿง  2. In this code:
for (let i = 0; i < arr.length; i += size)

  • let i = 0 โ†’ start from index 0
  • i < arr.length โ†’ keep looping until i reaches the end of the array
  • i += size โ†’ instead of increasing i by 1, we increase it by size That means each time, the loop jumps forward by size elements.

๐Ÿ’ก 3. Example to visualize
Suppose:

arr = [1, 2, 3, 4, 5, 6, 7]
size = 3

Enter fullscreen mode Exit fullscreen mode

Hereโ€™s how the loop runs:

Iteration i Value Slice (arr.slice(i, i + size)) Result
1 0 arr.slice(0, 3) โ†’ [1, 2, 3] result = [[1,2,3]]
2 3 arr.slice(3, 6) โ†’ [4, 5, 6] result = [[1,2,3],[4,5,6]]
3 6 arr.slice(6, 9) โ†’ [7] result = [[1,2,3],[4,5,6],[7]]

After that, i = 9, which is greater than arr.length (7), so the loop stops.

โœ… Final Thoughts

These questions are simple yet powerful for testing problem-solving and array manipulation in JavaScript.

If you havenโ€™t already, check out my previous parts:
๐Ÿ‘‰ Part 1
๐Ÿ‘‰ Part 2
๐Ÿ‘‰ Part 3
๐Ÿ‘‰ Part 4
๐Ÿ‘‰ Part 5
๐Ÿ‘‰ Part 6
๐Ÿ‘‰ Part 7

Top comments (0)