The 'Fizz Buzz' Coding Interview Problem in depth guide!
- 26-05-2018
- programming
Share:
Copy
The 'fizz' and 'Buzz' coding problem have been asked in many coding interviews with some modifications and is used to test the programmers problem solving abilities and understanding of algorithms and data structures.
The core knowledge that is required to solve these kinds of problems is understanding how to use the modulo(%) operator. The modulo(%) oprator returns the reminder when inserted between two numbers by dividing them. Below is the example to illustrate.
So what is the 'Fizz' and 'Buzz' problem, let's break it up. You are given a range of numbers from 1 upto 31 let's say, though it can be a very large number. Now from the beginning of the numbers whenever a number comes which is a multiple of 3 print the word 'Fizz' and whenever a number appears which is a multiple of 5 print 'Buzz'. Also print 'Fizz Buzz' whenever a number appears which is a multiple of both 3 and 5. If the number is not a multiple of both 3 and 5 just print the number. Although the number 3 and 5 are commonly used but the problem generally applies to any two numbers. Here we used 3 and 5.
To simplify:
print 'Fizz' if x
is multiple of 3.
print 'Buzz' if x
is multiple of 5.
print 'Fizz Buzz' if x
is multiple of both 3 and 5.
print 'x' if x
is not multiple of both 3 and 5.
Where x
is a number from the given range of numbers. In our case from 1 to 31
So any beginner will directly jump to the problem and start writing the code using if
blocks. The first step is to check if x
is multiple of 3. It can be easily done using the modulo(%) operator as when x
is divided by 3 modulo(%) operator will return the remainder. We just need to check when the remainder is 0 by that we will know that it is a multiple of 3.
The same is true for 5 also.
The first code everyone comes up with mostly looks like the code below:
It looks preety and should work dosen't it ?
Let's run the code. Here is what comes the output.
It looks good dosen't it? well the beginning is good as it prints 'Fizz' in place of 3 which is a multiple of 3 and also prints 'Buzz' in place of 5 as it is a multiple of 5 and so on but wait it prints 'Fizz' in place of 15 but it should have printed 'Fizz Buzz' as it is a multiple of both 3 and 5, so what's wrong here let's analyze further.
Well the problem is how we see the code and how the computer sees it. We as a human make errors and mistakes but the computer will always do exactly what it has been instructed to do. The above code is a perfect example of what's a bug is in programming, There was no errors and warnings in the code but we have got a result which is not what we expected and this is what we call a bug in a program or code script. Let's see where we made the mistake and how to fix the bug.
The program works in the following ways:
- Checks if
x
is a multiple of 3 or not. If yes then it prints 'Fizz' if not then it goes to anotherelif
block. - Checks if
x
is a multiple of 5 or not. If yes then it prints 'Buzz' if not then it moves to anotherelif
block. - Checks if
x
is a multiple of both 3 and 5 if yes then prints 'Fizz Buzz' if not then it moves to theelse
block. - The
else
block only printsx
.
The bug in our function fizzBuzz1
is that we first check if the number is multiple of 3 or not if not then only move on to the next step but if it's True then it won't check for another if
and elif
statements as it already found the result and the program directly jumps back to the beginning of the loop.
This is the case for 15 as the code breaks on 1st condition above, because it found 15 as multiple of 3 and goes back to the top of the loop without looking at the instructions below it.
Let's make a quick modification to our above code to make it work perfectly. We can just swap the position of the third condition statement which checks for x
when it's multiple of both 3 and 5. Below is our new code.
In our new function fizzBuzz2
it first checks for the condition when x
is multiple of both 3 and 5, if not then only it will check for x
being multiple of only 3 or only 5. If all conditions fails then it will simply print x
.
Lets run the code and check our new result.
Though its very simple but works absolutely fine and also efficient for larger values or ranges. Although the code can be improved and also using C/C++ can increase performance. There are also more cleaver ways to solve the problem but this is the most basic and easy to understand.