Coding Problems and Solutionss you'd find in company interviews.

1. Palindrome Checker

Problem: Write a function that takes a string as input and checks if it's a palindrome. A palindrome is a word or sentence that's spelled the same way forward and backward (ignoring spaces, punctuation, and capitalization).

Input: "A man, a plan, a canal, Panama"

Output: true

Hint: Consider using regex to clean the string before checking.


function isPalindrome(str) {
    // Your code here
}
        

2. Array Chunking

Problem: Write a function that splits an array into groups of a specified size and returns an array of these groups.

Input: chunkArray([1, 2, 3, 4, 5, 6, 7, 8], 3)

Output: [[1, 2, 3], [4, 5, 6], [7, 8]]

Hint: You'll need to loop through the array and push slices of it into a new array.


function chunkArray(arr, size) {
    // Your code here
}
        

3. Caesar Cipher

Problem: Implement a Caesar cipher, which is a type of substitution cipher where each letter in the plaintext is shifted a certain number of places down or up the alphabet.

Input: caesarCipher('hello', 3)

Output: 'khoor'

Hint: Consider the character codes to shift letters.


function caesarCipher(str, shift) {
    // Your code here
}
        

4. Find the Missing Number

Problem: Given an array containing n distinct numbers in the range [0, n], find the one number that is missing from the array.

Input: findMissingNumber([3, 0, 1])

Output: 2

Hint: You can solve this with a mathematical approach (e.g., using the sum formula) or by using bit manipulation.


function findMissingNumber(arr) {
    // Your code here
}
        

5. Flatten a Nested Array

Problem: Write a function that takes an array with nested arrays and returns a new array with all elements flattened into a single level.

Input: flattenArray([1, [2, [3, [4]], 5]])

Output: [1, 2, 3, 4, 5]

Hint: You can use recursion or JavaScript's Array.prototype.flat().


function flattenArray(arr) {
    // Your code here
}
        

6. Sum of Primes

Problem: Write a function that returns the sum of all prime numbers up to and including a given number.

Input: sumPrimes(10)

Output: 17 (because 2 + 3 + 5 + 7 = 17)

Hint: You’ll need to write a helper function to check for primality.


function sumPrimes(num) {
    // Your code here
}
        

7. Longest Word in a String

Problem: Write a function that finds the longest word in a string and returns its length.

Input: findLongestWordLength("The quick brown fox jumped over the lazy dog")

Output: 6 (because "jumped" is the longest word)

Hint: Consider splitting the string into words and checking each length.


function findLongestWordLength(str) {
    // Your code here
}
        

8. Array Diff

Problem: Write a function that takes two arrays and returns an array of the elements that are unique to each array (elements that are not in the intersection).

Input: arrayDiff([1, 2, 3], [2, 3, 4])

Output: [1, 4]

Hint: Use array methods like filter() and includes() to compare the arrays.


function arrayDiff(arr1, arr2) {
    // Your code here
}
        

9. Telephone Number Validator

Problem: Write a function that checks if a given string is a valid US phone number.

Input: telephoneCheck("555-555-5555")

Output: true

Hint: A valid US phone number has 10 or 11 digits and can be in various formats.


function telephoneCheck(str) {
    // Your code here
}
        

10. Roman Numeral Converter

Problem: Write a function to convert a number to a Roman numeral.

Input: convertToRoman(36)

Output: "XXXVI"

Hint: You'll need to map Roman numerals to their respective values and handle cases where subtraction is needed (like IV for 4).


function convertToRoman(num) {
    // Your code here
}
        
Powered by Blogger.