Multiple Way to Solve Fizz Buzz in Javascript

This is the first post, I want to share about fizz buzz implementation. Fizz buzz is a very simple programming task. And programming interviews are still one of the main methods of selecting qualified candidates at companies. Therefore, there are several questions that are often asked by interviewers to candidates. One of the questions that are sometimes asked is Fizz Buzz.


Usually, a typical question of fizz buzz is like this:

Write a program that prints out the numbers from 1 to n and for the multiples of 3 print `Fizz`, for the multiples of 5 print `Buzz`, and for the multiples of 3 and 5 print ‘Fizz Buzz’.

Example:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, …


Why use Fizz Buzz questions for interviewing the candidates?

This question is known for its simplicity in screening interview candidates. Historically, this question was asked so interviewers could easily screen out people (especially undergraduates) who couldn't program at all. 

Writing a program to output the first 100 FizzBuzz numbers is a relatively trivial problem requiring little more than a loop and conditional statements. However, its value in coding interviews is to analyze fundamental coding habits that may be indicative of overall coding ingenuity.


We can solve this with this code:



From the code we can get the output like this:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, …


In this program, we iterate over the integer i from 1 - n. It will check the i if i is divisible by 3 and 5, then print FizzBuzz. Otherwise, check if i is divisible by 3 and print Fizz if so. If i is also not divisible by 3, then check whether i is divisible by 5. If yes, then print Buzz. If not, then print the number i itself.


The modulo operation is a very costly operation compared to other arithmetic operations and i%15 is interpreted somehow like i%3 and i%5 hence it is a costly way to solve the problem in terms of time complexity. We can try to solve the same code using a simple addition operation by sacrificing an extra variable space. 


Here is the optimized code:




With the output like this:
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, ...

We can try to train with a variation of fizz buzz, to deepen the understanding of the fizz buzz implementation.
For the example variation like this:
  1. Change the fizz buzz word to other words
  2. Replace the number that contains 3 or 5
  3. Change Fizz for an odd number or Buzz for an even number
  4. Add another number that is multiple of (ex. 7 or 9), and change to other words


Thank you
See you in the next post!

Comments