# FizzBuzz, Array Chunk and Anagrams

Please check part 1 of this blog for more exercise.

In this second part of the blog, I want to start with one of the most common interview question, FizzBuzz.

The question:

Write a program that console logs the numbers from 1 to n. But for multiples of three print “fizz” instead of the number and the multiples of five print “buzz”. For numbers which are multiples of both three and five print “fizzbuzz”.

Example:

In this example, we need first to know how to find a multiple of a number, in programming we have the remainder operator (`%`) which returns the remainder left over when one operand is divided by a second operand. So if the number is a multiple of another number will return zero.

# Solution

1. Create a for loop that goes from 0 to the N number
2. Use an if, else statement to check each number if they are multiple of the current number.
3. If the number is multiple of the 3 print “fizz” if multiple of 5 “Buzz” and “FizzBuzz” if multiple of both.

The code:

Remember that if a number is multiple of 5 and 3 mean that is a multiple of 15. We could have used something like this :

This gives the same result, is import to check first if the number if multiple of both and then check the single one later because if the number is multiple of both and encounter first 3 or 5, the condition will return true, and will never run the condition where is a multiple of both.

# Array Chunk

The problem:

Given an array and chunk size, divide the array into many subarrays where each subarray is of length size.

Example:

This problem is probably easier to understand if you look at the example. So we are given an array and a number as an argument, we need to divide the whole array in the smaller array by the size of the N number we are getting.

Case to consider:

1. After assigning each array, we still left with some numbers to allocate
2. The number given is bigger than the number of elements inside the array.

# Solution

1. Create an empty array which represents the chunked array
2. loop inside each element of the array we are getting as an argument
3. Grap the last element inside the chunked array and store it into a variable
4. Create a conditional statement inside the loop
5. The condition should check if the last element in the chunked array does not exist or the last element in the chunked array has a lent equal to the N number we are getting.
6. If the above condition is true we need to push inside the chunked array an array with the element inside our current round of loop. In case is not we need to push the element inside the last element in the chunked array.

Code:

Looking at the code is much easier to understand the problem, as you can see inside our loop we are trying to find always the last element if this last element does not exist, means that we are in our first loop. The chunked array is still empty “[]”, so in this case, we are pushing inside an array with the current element inside “[]”. Let say for example our N number is 2 mean that the subarray should not have a length size ==then 2.

In this case, in the second loop, we are going to see if the last element exists, and in this case, it does, and if the length of this last element is equal to 2 which is not because we have 1 element inside, so the size is 1. Now the second condition will run “else”, and we are going to push inside the current element of the array inside our subarray:

In the third round of our last element exists and the size is equal to N number which is 2, now we are going to rerun the first condition pushing inside the array a new array with the current element inside. By repeating this process, we are going to get our chunked array solution.

# Anagrams

Question:

Check to see if two provided strings are anagrams of each other. One string is an anagram of another if it uses the same characters in the same quantity. Only consider characters, not spaces or punctuation. Consider capital letters to be the same as lower case.

Example:

The problem seems more complicated than what really is at first look, and there are many different solutions, but I would like to show you the more easy and logical one.

# Solution

1. For each string clear all the spaces inside and lower any capital letter
2. Transform the string into an array of characters.
3. Sort the element inside the array: cba → abc
4. Rejoin the sorted array into a new string
5. comparing both sorted string if they are equal. If they are equal return true or false if they are not equal.

# The code

We could have used a helper function to reduce the code repetition in both strings, for example:

As you can see, there is a straightforward solution by sorting each character and create a new string and comparing them.

I hope you enjoyed this new classic interview questions, and let me know if you have any better solution which I can publish :).