Flat Preloader Icon

Interview Programs

1.Reverse a String:

Write a function that takes a string as input and returns the string reversed. For example, if the input is “hello”, the output should be “olleh”.

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

console.log(reverseString('hello')); 
// Output: 'olleh'

				
			

2.Check for Palindrome:

Write a function that takes a string as input and returns true if the string is a palindrome (reads the same forwards and backwards), and false otherwise..

				
					function isPalindrome(str) {
    const reversed = str.split('')
    .reverse().join('');
    return str === reversed;
}

console.log(isPalindrome('racecar'));
// Output: true

				
			

3.Find the Maximum Number in an Array:

Write a function that takes an array of numbers as input and returns the maximum number in the array.

				
					function findMax(arr) {
    return Math.max(...arr);
}

console.log(findMax([3, 6, 2, 8, 1])); 
// Output: 8

				
			

4.Calculate Factorial of a Number:

Write a function that takes a number as input and returns the factorial of that number. The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.

				
					function factorial(n) {
    if (n === 0 || n === 1) return 1;
    return n * factorial(n - 1);
}

console.log(factorial(5)); 
// Output: 120

				
			

5.Check for Prime Number:

Write a function that takes a number as input and returns true if the number is prime (greater than 1 and divisible only by 1 and itself), and false otherwise.

				
					function isPrime(n) {
    if (n <= 1) return false;
    for (let i = 2; i <= Math.sqrt(n); i++) {
        if (n % i === 0) return false;
    }
    return true;
}

console.log(isPrime(11)); 
// Output: true

				
			

6.Remove Duplicates from an Array:

Write a function that takes an array as input and returns a new array with duplicate elements removed, preserving the original order.

				
					function removeDuplicates(arr) {
    return Array.from(new Set(arr));
}

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

				
			

7.Check for Armstrong Number:

Write a function that takes a number as input and returns true if the number is an Armstrong number, and false otherwise. An Armstrong number (also known as narcissistic number) is a number that is equal to the sum of its own digits raised to the power of the number of digits.

				
					function isArmstrong(num) {
    const digits = String(num).split('');
    const n = digits.length;
    const sum = digits.reduce((acc, digit) 
    => acc + Math.pow(parseInt(digit), n), 0);
    return sum === num;
}

console.log(isArmstrong(153));
// Output: true

				
			

8.Calculate Fibonacci Series:

Write a function that takes a number n as input and returns an array containing the first n numbers of the Fibonacci sequence. Each number in the Fibonacci sequence is the sum of the two preceding numbers.

				
					function fibonacci(n) {
    const fib = [0, 1];
    for (let i = 2; i < n; i++) {
        fib.push(fib[i - 1] + fib[i - 2]);
    }
    return fib;
}

console.log(fibonacci(5)); 
// Output: [0, 1, 1, 2, 3]

				
			

9.Check for Anagrams:

 Write a function that takes two strings as input and returns true if the strings are anagrams of each other (contain the same characters in a different order), and false otherwise.

				
					function isAnagram(str1, str2) {
    return str1.split('').sort().join('') 
    === str2.split('').sort().join('');
}

console.log(isAnagram('listen', 'silent')); 
// Output: true

				
			

10.Check for Armstrong Numbers in a Range:

Write a function that takes two numbers min and max as input and returns an array containing all Armstrong numbers in the range from min to max (inclusive).

				
					function armstrongInRange(min, max) {
    const armstrongNumbers = [];
    for (let i = min; i <= max; i++) {
        let sum = 0;
        let temp = i;
        const n = String(i).length;
        while (temp > 0) {
            const digit = temp % 10;
            sum += digit ** n;
            temp = Math.floor(temp / 10);
        }
        if (sum === i) armstrongNumbers.push(i);
    }
    return armstrongNumbers;
}

console.log(armstrongInRange(100, 1000)); 
// Output: [153, 370, 371, 407]