JavaScript Coding Round
20 Most Important Coding Problems with Solutions & Explanations
1. Reverse a String
Explanation: Uses split() to convert the string into an array, reverse() to reverse it, and join() to convert it back into
a string.
function reverseString(str) {
return [Link]("").reverse().join("");
}
[Link](reverseString("hello")); // olleh
2. Check Palindrome
Explanation: A palindrome reads the same forward and backward.
function isPalindrome(str) {
return str === [Link]("").reverse().join("");
}
[Link](isPalindrome("madam")); // true
3. Find Largest Number
Explanation: Iterates through the array and keeps updating the largest value.
const arr = [10, 5, 30, 8, 20];
let largest = arr[0];
for (let num of arr) {
if (num > largest) largest = num;
}
[Link](largest); // 30
4. Find Second Largest Number
Explanation: Removes duplicates and sorts numbers in descending order.
const arr = [10, 5, 30, 8, 20];
const result = [...new Set(arr)].sort((a, b) => b - a);
[Link](result[1]); // 20
5. Remove Duplicates
Explanation: Set stores only unique values.
const arr = [1, 2, 2, 3, 4, 4, 5];
const unique = [...new Set(arr)];
[Link](unique); // [1, 2, 3, 4, 5]
6. Count Frequency of Elements
Explanation: reduce() creates an object containing the count of every element.
const arr = ["apple", "banana", "apple", "orange"];
const result = [Link]((acc, item) => {
acc[item] = (acc[item] || 0) + 1;
return acc;
}, {});
[Link](result);
7. Find Duplicate Elements
Explanation: If the first index of an element is different from its current index, it is repeated.
const arr = [1, 2, 3, 2, 4, 1];
const duplicates = [Link]((item, index) =>
[Link](item) !== index
);
[Link]([...new Set(duplicates)]); // [2, 1]
8. FizzBuzz
Explanation: A classic logic problem using the modulo operator.
for (let i = 1; i <= 20; i++) {
if (i % 3 === 0 && i % 5 === 0) {
[Link]("FizzBuzz");
} else if (i % 3 === 0) {
[Link]("Fizz");
} else if (i % 5 === 0) {
[Link]("Buzz");
} else {
[Link](i);
}
}
9. Find Even and Odd Numbers
Explanation: filter() selects values based on a condition.
const arr = [1, 2, 3, 4, 5, 6];
const even = [Link](num => num % 2 === 0);
const odd = [Link](num => num % 2 !== 0);
[Link](even);
[Link](odd);
10. Sum of Array
Explanation: reduce() accumulates all array values into one result.
const arr = [1, 2, 3, 4, 5];
const sum = [Link]((total, num) => total + num, 0);
[Link](sum); // 15
11. Find Missing Number
Explanation: The difference between the expected sum and actual sum gives the missing number.
const arr = [1, 2, 3, 5];
const n = 5;
const expected = n * (n + 1) / 2;
const actual = [Link]((sum, num) => sum + num, 0);
[Link](expected - actual); // 4
12. Group Employees by Department
Explanation: Very common real-world problem: grouping employees, products, orders, or users by a property.
const employees = [
{ name: "Arun", department: "IT" },
{ name: "Rahul", department: "HR" },
{ name: "Kiran", department: "IT" }
];
const grouped = [Link]((acc, employee) => {
const dept = [Link];
if (!acc[dept]) acc[dept] = [];
acc[dept].push(employee);
return acc;
}, {});
[Link](grouped);
13. Flatten an Array
Explanation: flat(Infinity) converts nested arrays into a single-level array.
const arr = [1, [2, [3, 4]], 5];
[Link]([Link](Infinity));
// [1, 2, 3, 4, 5]
14. Capitalize Every Word
Explanation: Splits the sentence into words, transforms each word, then joins them.
function capitalizeWords(str) {
return str
.split(" ")
.map(word => word[0].toUpperCase() + [Link](1))
.join(" ");
}
[Link](capitalizeWords("hello world"));
// Hello World
15. Find Common Elements
Explanation: filter() keeps only values that are present in both arrays.
const arr1 = [1, 2, 3, 4];
const arr2 = [3, 4, 5, 6];
const common = [Link](num => [Link](num));
[Link](common); // [3, 4]
16. Check Anagram
Explanation: Two strings are anagrams if they contain the same characters with the same frequency.
function isAnagram(str1, str2) {
const a = [Link]("").sort().join("");
const b = [Link]("").sort().join("");
return a === b;
}
[Link](isAnagram("listen", "silent")); // true
17. First Non-Repeating Character
Explanation: The first character whose first and last positions are the same is unique.
function firstNonRepeating(str) {
for (let char of str) {
if ([Link](char) === [Link](char)) {
return char;
}
}
return null;
}
[Link](firstNonRepeating("aabbcde")); // c
18. Factorial of a Number
Explanation: Multiplies all numbers from 1 to n.
function factorial(n) {
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}
[Link](factorial(5)); // 120
19. Fibonacci Series
Explanation: Each number is the sum of the previous two numbers.
function fibonacci(n) {
let a = 0, b = 1;
for (let i = 0; i < n; i++) {
[Link](a);
let next = a + b;
a = b;
b = next;
}
}
fibonacci(7);
20. Debounce Function
Explanation: Used in search boxes and API calls to wait until the user stops typing before executing a function.
function debounce(func, delay) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
func(...args);
}, delay);
};
}