Solving Common LeetCode Questions in JavaScript, Ruby, and C++ : Fizz Buzz
If you are venturing into the professional world of coding surely you have heard about the ominous “coding interview”. You sit in a room for 30 minutes, maybe an hour, you get a question, and you have to figure it out on the spot. One of the most common coding interview questions is Fizz Buzz, here is the prompt taken from LeetCode:
Basically, given a number n, return an array of strings from 1 through n (inclusive) with the above qualifications: if the number is divisible by 3 and 5 return the word “FizzBuzz”, if the number is divisible by 3 return “Fizz”, if the number is divisible by 5 return “Buzz”, otherwise return the number (as a string). Let’s walk through it in steps.
1. Setup a function that takes an integer as a parameter and returns a string array:
This step is straight forward, let’s make the skeleton of the function that will solve this prompt.
JavaScript:
Ruby:
C++:
2. Setup our loop, loop n times:
We are not sure exactly how we are solving the problem just yet, but we know we need to do something 1 through n times. Let’s get our loops setup.
JavaScript:
Ruby:
Here we are using a while loop and a counter. We start the counter at 1 (remember, the prompt is 1 through n ) and we are going to do something until counter has exceeded the value of n.
C++:
Back to the trusty for loop. There are many different ways to loop, whichever way you are comfortable with is fine for solving this problem. Personally, I like working with for loops.
3. Add “FizzBuzz” to the answer array if the number is divisible by 3 and 5:
Now we have to start using our noggin a bit. To determine if a number is divisible by another we use the modulo operator. If n % 3 == 0 we know that n is divisible by 3 because there is no remainder. Also remember back to your math days, if a number is divisible by two different numbers, like 3 and 5, it is also divisible by their product. So, if a number is divisible by 3 and 5, it is also divisible by 15. This will save us a little code.
JavaScript:
Ruby:
C++:
Our first check is done: if a number is divisible by 3 and 5 (hence divisible by 15) we will push the string FizzBuzz into our answer array. Now we need to implement this for our other checks.
4. Add “Fizz” to the answer array if the number is divisible by 3:
We are repeating the same thing we did above, but for the number 3. It is important to note that we are in an else if here, if a number is divisible by 15 it will already have been caught and FizzBuzz will be pushed into the answer array, we don’t want to also add Fizz into the answer array so we use an else if.
JavaScript:
Ruby:
C++:
5. Add “Buzz” to the answer array if the number is divisible by 5:
Again repeat what we did above, this time for 5.
JavaScript:
Ruby:
C++:
6. Otherwise add the number (as a string) to the answer array:
If the number is not divisible by 3, 5, or 15 we add the number into the answer array with an else at the end of our else if chain, this will only execute if all the other checks returned false, so no worry about duplicates. Remember to cast the number to a string before pushing it into the answer array.
JavaScript:
Ruby:
C++:
And we’ve done it! Our function takes in an argument of n, loops over the numbers 1 through n, if the number is divisible by 15 we push “FizzBuzz” into our answer array, if the number is divisible by 3 we push “Fizz” into our answer array, if the number is divisible by 5 we push “Buzz” into our answer array and if none of the above is true we cast the number to a string and push said string into our answer array!.Hopefully this was helpful in not only solving the problem but in demonstrating how to go about actually tackling this problem. Stay tuned for more LeetCode solutions in the future!