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.
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”.
fizzBuzz(5) output: 1, 2, fizz, 4, buzz
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.
- Create a for loop that goes from 0 to the N number
- Use an if, else statement to check each number if they are multiple of the current number.
- If the number is multiple of the 3 print “fizz” if multiple of 5 “Buzz” and “FizzBuzz” if multiple of both.
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 :
if(i % 3 === 0 && i % 5===0)
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.
Given an array and chunk size, divide the array into many subarrays where each subarray is of length size.
chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], ]chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], ]chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
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:
- After assigning each array, we still left with some numbers to allocate
- The number given is bigger than the number of elements inside the array.
- Create an empty array which represents the chunked array
- loop inside each element of the array we are getting as an argument
- Grap the last element inside the chunked array and store it into a variable
- Create a conditional statement inside the loop
- 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.
- 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.
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:
last.push(4) = [[1,4]]
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.
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.
anagrams('rail safety', 'fairy tales') --> Trueanagrams('RAIL! SAFETY!', 'fairy tales') --> Trueanagrams('Hi there', 'Bye there') --> False
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.
- For each string clear all the spaces inside and lower any capital letter
- Transform the string into an array of characters.
- Sort the element inside the array: cba → abc
- Rejoin the sorted array into a new string
- comparing both sorted string if they are equal. If they are equal return true or false if they are not equal.
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 :).