Skip to content

9. Coding Questions Practice ​

Coding questions are extremely important in frontend and JavaScript interviews.

Interviewers check:

  • Problem-solving
  • JavaScript fundamentals
  • Array manipulation
  • Logic building
  • Optimization skills

Practice these questions regularly.


Most Important Coding Questions ​

  • Reverse String
  • Palindrome
  • Remove Duplicates
  • Flatten Array
  • Debounce Function
  • Throttle Function
  • Custom map()
  • Custom filter()
  • Custom reduce()
  • Fibonacci
  • Deep Clone
  • Group By Object Key

1. Reverse String ​

Reverse characters of a string.


Example ​

Input:

js
"hello";

Output:

js
"olleh";

Solution 1 — Using Built-in Methods ​

js
function reverseString(str) {
  return str.split("").reverse().join("");
}

console.log(reverseString("hello"));

Output ​

js
olleh;

Explanation ​

  • split("") converts string → array
  • reverse() reverses array
  • join("") converts array → string

Solution 2 — Without Built-in Reverse ​

js
function reverseString(str) {
  let reversed = "";

  for (let i = str.length - 1; i >= 0; i--) {
    reversed += str[i];
  }

  return reversed;
}

console.log(reverseString("hello"));

2. Palindrome ​

Palindrome reads same forward and backward.


Example ​

js
madam;
racecar;

Solution ​

js
function isPalindrome(str) {
  const reversed = str.split("").reverse().join("");

  return str === reversed;
}

console.log(isPalindrome("madam"));

Output ​

js
true;

3. Remove Duplicates from Array ​


Example ​

Input:

js
[1, 2, 2, 3, 4, 4];

Output:

js
[1, 2, 3, 4];

Solution Using Set ​

js
function removeDuplicates(arr) {
  return [...new Set(arr)];
}

console.log(removeDuplicates([1, 2, 2, 3, 4, 4]));

Output ​

js
[1, 2, 3, 4];

4. Flatten Array (VERY IMPORTANT) ​

Convert nested array into single array.


Example ​

Input:

js
[1, [2, 3], [4, [5]]];

Output:

js
[1, 2, 3, 4, 5];

Solution Using flat() ​

js
const arr = [1, [2, 3], [4, [5]]];

console.log(arr.flat(Infinity));

Recursive Solution ​

js
function flattenArray(arr) {
  let result = [];

  for (let item of arr) {
    if (Array.isArray(item)) {
      result = result.concat(flattenArray(item));
    } else {
      result.push(item);
    }
  }

  return result;
}

console.log(flattenArray([1, [2, 3], [4, [5]]]));

5. Debounce Function (VERY IMPORTANT) ​

Delays function execution until user stops triggering event.


Example ​

js
function debounce(fn, delay) {
  let timer;

  return function (...args) {
    clearTimeout(timer);

    timer = setTimeout(() => {
      fn(...args);
    }, delay);
  };
}

Common Uses ​

  • Search bar
  • API optimization
  • Resize events

6. Throttle Function (VERY IMPORTANT) ​

Limits function execution rate.


Example ​

js
function throttle(fn, delay) {
  let lastCall = 0;

  return function (...args) {
    const now = Date.now();

    if (now - lastCall >= delay) {
      lastCall = now;

      fn(...args);
    }
  };
}

Common Uses ​

  • Scroll events
  • Resize events
  • Mouse movement

Difference Between Debounce and Throttle ​

DebounceThrottle
Waits before executingExecutes at intervals
Best for search inputBest for scroll events
Reduces unnecessary callsLimits execution frequency

7. Custom map() Polyfill (VERY IMPORTANT) ​


Example ​

js
Array.prototype.myMap = function (callback) {
  const result = [];

  for (let i = 0; i < this.length; i++) {
    result.push(callback(this[i], i, this));
  }

  return result;
};

const nums = [1, 2, 3];

const output = nums.myMap((num) => num * 2);

console.log(output);

Output ​

js
[2, 4, 6];

8. Custom filter() Polyfill ​


Example ​

js
Array.prototype.myFilter = function (callback) {
  const result = [];

  for (let i = 0; i < this.length; i++) {
    if (callback(this[i], i, this)) {
      result.push(this[i]);
    }
  }

  return result;
};

const nums = [1, 2, 3, 4];

const even = nums.myFilter((num) => num % 2 === 0);

console.log(even);

Output ​

js
[2, 4];

9. Custom reduce() Polyfill ​


Example ​

js
Array.prototype.myReduce = function (callback, initialValue) {
  let accumulator = initialValue;

  for (let i = 0; i < this.length; i++) {
    accumulator = callback(accumulator, this[i], i, this);
  }

  return accumulator;
};

const nums = [1, 2, 3, 4];

const sum = nums.myReduce((acc, curr) => acc + curr, 0);

console.log(sum);

Output ​

js
10;

10. Fibonacci Series ​

Each number is sum of previous two numbers.


Example ​

txt
0 1 1 2 3 5 8

Solution ​

js
function fibonacci(n) {
  let result = [0, 1];

  for (let i = 2; i < n; i++) {
    result.push(result[i - 1] + result[i - 2]);
  }

  return result;
}

console.log(fibonacci(7));

Output ​

js
[0, 1, 1, 2, 3, 5, 8];

11. Deep Clone (VERY IMPORTANT) ​

Creates completely independent object copy.


Example ​

js
const user = {
  name: "Dipak",
  address: {
    city: "Kolkata",
  },
};

const deepCopy = structuredClone(user);

deepCopy.address.city = "Delhi";

console.log(user.address.city);

Output ​

js
Kolkata;

Alternative Method ​

js
const copy = JSON.parse(JSON.stringify(user));

Limitation ​

Does not properly copy:

  • Functions
  • Dates
  • undefined

12. Group By Object Key (VERY IMPORTANT) ​

Very common interview question.


Example ​

Input:

js
const users = [
  { name: "Dipak", city: "Kolkata" },
  { name: "Rahul", city: "Delhi" },
  { name: "Amit", city: "Kolkata" },
];

Expected Output ​

js
{
  Kolkata: [
    { name: "Dipak", city: "Kolkata" },
    { name: "Amit", city: "Kolkata" }
  ],

  Delhi: [
    { name: "Rahul", city: "Delhi" }
  ]
}

Solution ​

js
function groupBy(arr, key) {
  return arr.reduce((acc, curr) => {
    const group = curr[key];

    if (!acc[group]) {
      acc[group] = [];
    }

    acc[group].push(curr);

    return acc;
  }, {});
}

const users = [
  { name: "Dipak", city: "Kolkata" },
  { name: "Rahul", city: "Delhi" },
  { name: "Amit", city: "Kolkata" },
];

console.log(groupBy(users, "city"));

13. Count Character Frequency ​

Very common interview question.


Example ​

Input:

js
"hello";

Output:

js
{
  h: 1,
  e: 1,
  l: 2,
  o: 1
}

Solution ​

js
function countChars(str) {
  const result = {};

  for (let char of str) {
    result[char] = (result[char] || 0) + 1;
  }

  return result;
}

console.log(countChars("hello"));

14. Find Maximum Number in Array ​


Solution ​

js
const nums = [10, 20, 5, 40];

const max = Math.max(...nums);

console.log(max);

Without Built-in Method ​

js
function findMax(arr) {
  let max = arr[0];

  for (let num of arr) {
    if (num > max) {
      max = num;
    }
  }

  return max;
}

console.log(findMax([10, 20, 5, 40]));

15. Find Missing Number ​


Example ​

Input:

js
[1, 2, 3, 5];

Output:

js
4;

Solution ​

js
function findMissing(arr, n) {
  const total = (n * (n + 1)) / 2;

  const sum = arr.reduce((acc, curr) => acc + curr, 0);

  return total - sum;
}

console.log(findMissing([1, 2, 3, 5], 5));

16. Check Anagram ​

Two strings contain same characters.


Example ​

txt
listen → silent

Solution ​

js
function isAnagram(str1, str2) {
  const a = str1.split("").sort().join("");
  const b = str2.split("").sort().join("");

  return a === b;
}

console.log(isAnagram("listen", "silent"));

Output ​

js
true;

17. Reverse Number ​


Example ​

Input:

txt
1234

Output:

txt
4321

Solution ​

js
function reverseNumber(num) {
  return Number(num.toString().split("").reverse().join(""));
}

console.log(reverseNumber(1234));

18. Capitalize First Letter of Each Word ​


Example ​

Input:

txt
hello world

Output:

txt
Hello World

Solution ​

js
function capitalize(str) {
  return str
    .split(" ")
    .map((word) => {
      return word[0].toUpperCase() + word.slice(1);
    })
    .join(" ");
}

console.log(capitalize("hello world"));

Most Asked Coding Questions ​

Focus heavily on:

  • Debounce
  • Throttle
  • Polyfills
  • Flatten array
  • Group by
  • Array manipulation
  • String problems

Interview Tips ​

  • Practice writing logic without copying
  • Explain time complexity
  • Explain approach before coding
  • Write clean variable names
  • Handle edge cases

Best Practice for Preparation ​

Practice daily:

  1. Array questions
  2. Object manipulation
  3. Async JavaScript
  4. Polyfills
  5. String problems
  6. Output-based questions

These are repeatedly asked in frontend interviews.

© 2025 DDocs · Dipak's Documentation Guide