DEV Community

Cover image for Coding interview challenges
Huy Do
Huy Do

Posted on

Coding interview challenges

There is common interview process by asking you to solve the challenges on online platform or on the whiteboard. Here are 5 common interview questions for javaScript or front-end Junior Developer position.

Tacke Code Challege

  • Put in the time to prepare
    You should practice the code challenges online for algorithms and data structures as much as possible. These are free and paid for practice interview skill Pramp,
    interviewing.io,
    GeeksforGeeks, CodeSignal, Skilled, Interview Cake, HackerRank, freeCodeCamp.

  • Practice thinking alound
    Talking through your thought process in an interview while writing your solution. This will help interview a chance to help you if you make a wrong answer. It's also showed your communication skills.

  • Understand the problem before starting to code
    This is important because it will save your time to solve the wrong problem and you may come up with questions for the interviewer.

  • Pracice writing code hand
    This helps you get familiar with the whiteboard challenge because it will not provide autocomplete, format, error warning, etc. Try to write from left to right on the whiteboard and leave space so you can fill in more details.

Common Coding Javascript Challenges

1 Palindrome

A palindrome is a word, sentence, or another type of character sequence which reads the same backward as forward. For example, “racecar” and “Anna” are palindromes. “Table” and “John” aren’t palindromes, because they don’t read the same from left to right and from right to left.

You will be provided a string and return true or false.

palindrome('racecar')  ===  true
palindrome('table')  ===  false
Enter fullscreen mode Exit fullscreen mode

Reasoning about the challenge

This challenge revolves around the idea of reversing a string. If the reversed string is the same as the original input string, then you have a palindrome and your function should return true or return false otherwise.

Solution 1

const palindrome = str => {
  // turn the string to lowercase
  str = str.toLowerCase()
  // reverse input string and return the result of the
  // comparison
  return str === str.split('').reverse().join('')
}
Enter fullscreen mode Exit fullscreen mode

Solution 2

function palindrom(inputString) {
  let str = ""
  //copy from the end to front of input string
  for(let i = inputString.length; i >=0; i--) 
      str += inputString.charAt(i)
  //return true if both strings are matched
  return str === inputString
}
Enter fullscreen mode Exit fullscreen mode

Solution 3

function palindrome(str) {

  str = str.replace(/[\W_]/g, '').toLowerCase();
    for (var i = 0, len = str.length -1; i < len/2; i ++){
        if (str[i] !== str[len-i]){
            return false;
        }
    }
    return true;
 }

 palindrome("eye");
Enter fullscreen mode Exit fullscreen mode

2 FizzBuzz

Understanding the challenge

The FizzBuzz challenge goes something like this. Write a function that does the following:

  • console logs the numbers from 1 to n, where n is the integer the function takes as its parameter
  • logs fizz instead of the number for multiples of 3
  • logs buzz instead of the number for multiples of 5
  • logs fizzbuzz for numbers that are multiples of both 3 and 5
fizzBuzz(5)
// 1
// 2
// fizz
// 4
// buzz
Enter fullscreen mode Exit fullscreen mode

Reasoning about the challenge

One important point about FizzBuzz relates to how you can find multiples of a number in JavaScript. You do this using the modulo or remainder operator, %. This operator returns the remainder after a division between two numbers. A remainder of 0 indicates that the first number is a multiple of the second number

12 % 3 //0
12 % 5 //2
Enter fullscreen mode Exit fullscreen mode

Solution 1

const fizzBuzz = num => {
  for(let i = 1; i <= num; i++) {
    // check if the number is a multiple of 3 and 5
    if(i % 3 === 0 && i % 5 === 0) {
      console.log('fizzbuzz')
    } // check if the number is a multiple of 3
      else if(i % 3 === 0) {
      console.log('fizz')
    } // check if the number is a multiple of 5
      else if(i % 5 === 0) {
      console.log('buzz')
    } else {
      console.log(i)
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Solution 2

const fizzBuzz = num => {
  for (let i = 0; i < num; i++){
    let output = ""
    if (n % 3 == 0) output +="Fizz"
    if (n % 5 == 0) output +="Buzz"
    console.log(output)
  }
}
Enter fullscreen mode Exit fullscreen mode

3 Anagram

A word is an anagram of another word if both use the same letters in the same quantity, but arranged differently.

Understanding the challenge

You can state this challenge in the following terms: write a function that checks if two provided strings are anagrams of each other; letter casing shouldn’t matter. Also, consider only characters, not spaces or punctuation

anagram('finder', 'Friend')  // true
anagram('hi', 'hello') // false
Enter fullscreen mode Exit fullscreen mode

We can use javascript object literal to calculate the number of letters of both strings. The key is letter character and value is the number of times that letter appears in the given string. Converting string to lower case or upper case. removing any that isn't a character by using regular expression

Solution

// helper function that builds the
// object to store the data
const buildCharObject = str => {
  const charObj = {}
  for(let char of str.replace(/[^\w]/g).toLowerCase()) {
    // if the object has already a key value pair
    // equal to the value being looped over,
    // increase the value by 1, otherwise add
    // the letter being looped over as key and 1 as its value
    charObj[char] = charObj[char] + 1 || 1
  }
  return charObj
}

// main function
const anagram = (strA, strB) => {
  // build the object that holds strA data
  const aCharObject = buildCharObject(strA)
  // build the object that holds strB data
  const bCharObject = buildCharObject(strB)

  // compare number of keys in the two objects
  // (anagrams must have the same number of letters)
  if(Object.keys(aCharObject).length !== Object.keys(bCharObject).length) {
    return false
  }
  // if both objects have the same number of keys
  // we can be sure that at least both strings
  // have the same number of characters
  // Now we can compare the two objects to see if both
  // have the same letters in the same amount
  for(let char in aCharObject) {
    if(aCharObject[char] !== bCharObject[char]) {
      return false
    }
  }
  // if both the above checks succeed,
  // you have an anagram: return true
  return true
}
Enter fullscreen mode Exit fullscreen mode

4 Find the Vowels

Understanding the challenge

A function that takes a string as argument and returns the number of vowels contained in that string.

The vowels are “a”, “e”, “i”, “o”, “u”.

findVowels('there') // 2
findVowels('why') // 0
Enter fullscreen mode Exit fullscreen mode

Solution

const findVowels = str => {
  let count = 0
  const vowels = ['a', 'e', 'i', 'o', 'u']
  for(let char of str.toLowerCase()) {
    if(vowels.includes(char)) {
      count++
    }
  }
  return count
}
Enter fullscreen mode Exit fullscreen mode

5 Fibonacci

This article couldn’t be complete without a discussion of the Fibonacci challenge, a classic question you’ll surely come across during a job interview or coding practice.

A Fibonacci sequence is an ordering of numbers where each number is the sum of the preceding two. For example, the first ten numbers of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

Understanding the challenge

The Fibonacci challenge goes something like this: write a function that returns the nth entry in the Fibonacci sequence, where n is a number you pass in as argument to the function.

fibonacci(3)  // --> 2
Enter fullscreen mode Exit fullscreen mode

Solution 1

const fibonacci = num => {
  // store the Fibonacci sequence you're going
  // to generate inside an array and
  // initialize the array with the first two
  // numbers of the sequence
  const result = [0, 1]

  for(let i = 2; i <= num; i++) {
    // push the sum of the two numbers
    // preceding the position of i in the result array
    // at the end of the result array
    const prevNum1 = result[i - 1]
    const prevNum2 = result[i - 2]
    result.push(prevNum1 + prevNum2)
  }
  // return the last value in the result array
  return result[num]
}
Enter fullscreen mode Exit fullscreen mode

Solution 2

const fibonacci = num => {
  // if num is either 0 or 1 return num
  if(num < 2) {
    return num
  }
  // recursion here
  return fibonacci(num - 1) + fibonacci(num - 2)
}
Enter fullscreen mode Exit fullscreen mode

References
https://www.sitepoint.com/5-common-coding-interview-challenges/

Top comments (0)