JS Interviews... and how to. (Part 1)
Published: 2020-05-23 01:26:04

### Trailer
>Up until recently I was always struggling with Javascript interviews and within the last four months, I have spent countless minutes improving my interview skills and I have done and passed a few interviews.
>I felt it is only fair that I share some of the interesting questions that you may encounter during interviews. This first part I'll cover some common whiteboard questions.

> *Also I thought I should include the thought process towards solving some of these questions.*

The solutions shown below may not be the best, so if you have a better way of solving it, do check out my [github repo](https://github.com/kharioki/Js_interview_challenges) and send a *Pull Request* for the same.

## Sample Challenge Questions

Let's dive into some challenges you may encounter in your written or whiteboard interviews.
The solutions shown below assume that you have basic understanding of Javascript, specifically javascript data types for this first part.

*Challenge 1- Reverse a string*
> return a string in reverse. (while this may be a common whiteboard question, there are a few ways of solving this. Let's look at some.
```
// solution 1 - convert the string into an array of characters, reverse array then convert to string
function reverseString(str) {
  const strArr = str.split('');
  strArr.reverse();
  return strArr.join('');
}
```
```
function reverseString(str) {
  // solution 2 => using for loop and decreasing array
  let revString = '';
  for (let i = str.length - 1; i >= 0; i--) {
    revString = revString + str[i];
  }
  return revString;
}
```
```
function reverseString(str) {
  // solution 3 => using for loop and increasing array
  let revString = '';
  for (let i = 0; i <= str.length - 1; i++) {
    revString = str[i] + revString;
  }
  return revString;
}
```
```
function reverseString(str) {
  // solution 4 => using for-of loop (es6)
  let revString = '';
  for (let char of str) {
    revString = char + revString;
  }
  return revString;
}
```
```
function reverseString(str) {
  // solution 5 => using forEach
  let revString = '';
  str.split('').forEach(char => revString = char + revString);
  return revString;
}
```
```
function reverseString(str) {
  // solution 6 => using array reduce
  return str.split('').reduce((acc, char) => char + acc, '');
}
```

*Challenge 2- Validate a palindrome*
> Return true if palindrome. A palindrome is a word/phrase that reads the same backward as forwards e.g 'madam', 'nurses run', 'racecar'.

```
function isPalindrome(str) {
  // reverse string
  const reversedStr = str.split('').reduce((acc, char) => char + acc, '');
  // compare reversed string and initial string
  return reversedStr === str;
}
```

*Challenge 3- Reverse an integer*
> Return an integer in reverse e.g reverseInt(-2345) === -5432.

```
function reverseInt(int) {
  // convert integer to string first then reverse
  // to make sure this works even for negative numbers we multiply by Math.sign(int)
  const revString = int
    .toString()
    .split('')
    .reverse()
    .join('');
  return parseInt(revString) * Math.sign(int);
}
```

*Challenge 4 - Capitalize letters*
> Return a string with the first letter of every word capitalized.

```
// solution 1 - using for loop
function capitalizeLetters(str) {
  // first lowercase string
  const strArr = str.toLowerCase().split(' ');

  for (let i = 0; i < strArr.length; i++) {
    // capitalize every first letter
    strArr[i] =
      strArr[i].substring(0, 1).toUpperCase() + strArr[i].substring(1);
  }
  // join the words
  return strArr.join(' ');
}
```
```
// solution 2 - using array map
function capitalizeLetters(str) {
  return str
    .toLowerCase()
    .split(' ')
    .map(word => word[0].toUpperCase() + word.substring(1))
    .join(' ');
};
```
```
// solution 3 - using regex(regular expressions)
function capitalizeLetters(str) {
  return str
    .toLowerCase()
    .split(' ')
    .map(word => word[0].toUpperCase() + word.substring(1))
    .join(' ');
};
```

*Challenge 5 - Max character*
> Return the character that is most common in a string.

```
function maxCharacter(str) {
  const charObj = {};
  let maxNum = 0;
  let maxChar = '';

  str.split('').forEach(char => {
    // loop through the char array
    if (charObj[char]) {
      // if char already exist in charObj, increment
      charObj[char]++;
    } else {
      // else set value as 1
      charObj[char] = 1;
    }
  });

  // check for the key with maximum count
  for (let char in charObj) {
    if (charObj[char] > maxNum) {
      maxNum = charObj[char];
      maxChar = char;
    }
  }

  return maxChar;
}
```

*Challenge 6 - FizzBuzz*
> Write a program that prints all the numbers from 1 to 100.
> For multiples of 3, instead of the number, print "Fizz", for multiples of 5 print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz". - this is actually a rather easy one and it tests edge cases.

```
for (let i = 1; i <= 100; i++) {
    // check if number is divisible by both 3 and 5
    if (i % 3 === 0 && i % 5 === 0) {
      console.log('FizzBuzz');
    } else 
    // check if number is divisible by 3
    if (i % 3 === 0) {
      console.log('Fizz');
    } else 
    // check if number is divisible by 5
    if (i % 5 === 0) {
      console.log('Buzz');
    } else {
      console.log(i);
    }
  }
```

This write up is getting really long and I will continue it in Part 2.
You can check out the code on my [github repo](https://github.com/kharioki/Js_interview_challenges).

**Tip of the day**
> *ASK FOR HELP* - take-home assessments are common during interview process and maybe overwhelming especially if it's your first time. A simple trick I learned was, it is good to ask for help. After all, chances are you'll be working in a team if you do get the job.