Fizz Buzz Interview Question

If I was a hot shot head chef at a world class restaurant, I would use “The chicken test” as a litmus test for hiring chefs.

I would ask them to cook me chicken! Why chicken you may ask?

If a chef cannot cook chicken and make it tasty, then they probably are not ready to cook world class meals

Source: Victoria Shes via Unsplash.com

Same is applicable to a software engineer, if they cannot code the simple kids game of Fizz Buzz then, they are probably not ready to produce world class software!

So what is Fizz Buzz?

Fizz Buzz is a kids game played to strengthen their mathematical skills. Played by two people they each take a turn to count 1,2,3,4,5,6,…. but every time there is a multiple of 3 they say Fizz. And every time there is a multiple of 5 they say Buzz and with numbers such as 15 which is divisible to both 3 and 5 they say Fizz Buzz. Another exception is numbers which are not multiples of 3 or 5, then the kids will just call the number.

Let’s write a program that does this, and we will be using Python:

def fizzbuzz(i):
if i % 3 == 0:
return "Fizz"
if i % 5 == 0:
return "Buzz"
if (i % 3 == 0) and (i % 5 == 0):
return "FizzBuzz"
    return i
print(fizzbuzz())

In the code above tells our Python interpreter to check if our variable i is a multiple of 3, if it is then output Fizz.

If the statement is false move on to check if i is a multiple of 5, if it is then return Buzz.

If that statement is also false move on to check if i is a multiple of 3 and 5 and if it is return to us FizzBuzz.

If non of those are true, return to us i, which will most probably be a number. I ran this on Google colab and look at the outputs I got.

The screenshots above show i as 3,5 and 15. The first two outputs of 3 and 5 are correct, but the last one of 15 outputs Fizz and we are expecting Fizz Buzz.

This is a common problem with decision statements, it only moves to the next condition if one is False. But 15 is applicable to both statement 1 and 2, so our not so intelligent friend the Python interpreter chooses the first option and hence we get Fizz as our input.

We can also be lazy as our Python interpreter friend, and just switch our statements;

That’s sorted, it is indeed a working model, can it be improvised so that it prints 1 to 100 ?

def fizz_buzz(i):
for i in range(1,101):
output = ""
if i % 3 == 0:
output = "Fizz"
if i % 5 == 0:
output += "Buzz"
print(output or i)
print(fizz_buzz(100))

The code above is telling our Python interpreter to run through the numbers 1 to 100. Every time there is a multiple of 3, add Fizz to the empty string output.

Then move on to multiples of 5, and add Buzz to the string, no need o check for multiples of 3 and 5, as it will automatically check. mI also ran this on Google Colab:

Well you are most welcome to add to my code, I have attached it!

Additional Resources

Google Colaboratory


Fizz Buzz Interview Question was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Thenjiwe kubheka

If I was a hot shot head chef at a world class restaurant, I would use “The chicken test” as a litmus test for hiring chefs.

I would ask them to cook me chicken! Why chicken you may ask?

If a chef cannot cook chicken and make it tasty, then they probably are not ready to cook world class meals

Source: Victoria Shes via Unsplash.com

Same is applicable to a software engineer, if they cannot code the simple kids game of Fizz Buzz then, they are probably not ready to produce world class software!

So what is Fizz Buzz?

Fizz Buzz is a kids game played to strengthen their mathematical skills. Played by two people they each take a turn to count 1,2,3,4,5,6,…. but every time there is a multiple of 3 they say Fizz. And every time there is a multiple of 5 they say Buzz and with numbers such as 15 which is divisible to both 3 and 5 they say Fizz Buzz. Another exception is numbers which are not multiples of 3 or 5, then the kids will just call the number.

Let's write a program that does this, and we will be using Python:

def fizzbuzz(i):
if i % 3 == 0:
return "Fizz"
if i % 5 == 0:
return "Buzz"
if (i % 3 == 0) and (i % 5 == 0):
return "FizzBuzz"
    return i
print(fizzbuzz())

In the code above tells our Python interpreter to check if our variable i is a multiple of 3, if it is then output Fizz.

If the statement is false move on to check if i is a multiple of 5, if it is then return Buzz.

If that statement is also false move on to check if i is a multiple of 3 and 5 and if it is return to us FizzBuzz.

If non of those are true, return to us i, which will most probably be a number. I ran this on Google colab and look at the outputs I got.

The screenshots above show i as 3,5 and 15. The first two outputs of 3 and 5 are correct, but the last one of 15 outputs Fizz and we are expecting Fizz Buzz.

This is a common problem with decision statements, it only moves to the next condition if one is False. But 15 is applicable to both statement 1 and 2, so our not so intelligent friend the Python interpreter chooses the first option and hence we get Fizz as our input.

We can also be lazy as our Python interpreter friend, and just switch our statements;

That's sorted, it is indeed a working model, can it be improvised so that it prints 1 to 100 ?

def fizz_buzz(i):
for i in range(1,101):
output = ""
if i % 3 == 0:
output = "Fizz"
if i % 5 == 0:
output += "Buzz"
print(output or i)
print(fizz_buzz(100))

The code above is telling our Python interpreter to run through the numbers 1 to 100. Every time there is a multiple of 3, add Fizz to the empty string output.

Then move on to multiples of 5, and add Buzz to the string, no need o check for multiples of 3 and 5, as it will automatically check. mI also ran this on Google Colab:

Well you are most welcome to add to my code, I have attached it!

Additional Resources

Google Colaboratory


Fizz Buzz Interview Question was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Thenjiwe kubheka


Print Share Comment Cite Upload Translate Updates
APA

Thenjiwe kubheka | Sciencx (2021-08-22T20:22:02+00:00) Fizz Buzz Interview Question. Retrieved from https://www.scien.cx/2021/08/22/fizz-buzz-interview-question/

MLA
" » Fizz Buzz Interview Question." Thenjiwe kubheka | Sciencx - Sunday August 22, 2021, https://www.scien.cx/2021/08/22/fizz-buzz-interview-question/
HARVARD
Thenjiwe kubheka | Sciencx Sunday August 22, 2021 » Fizz Buzz Interview Question., viewed ,<https://www.scien.cx/2021/08/22/fizz-buzz-interview-question/>
VANCOUVER
Thenjiwe kubheka | Sciencx - » Fizz Buzz Interview Question. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/22/fizz-buzz-interview-question/
CHICAGO
" » Fizz Buzz Interview Question." Thenjiwe kubheka | Sciencx - Accessed . https://www.scien.cx/2021/08/22/fizz-buzz-interview-question/
IEEE
" » Fizz Buzz Interview Question." Thenjiwe kubheka | Sciencx [Online]. Available: https://www.scien.cx/2021/08/22/fizz-buzz-interview-question/. [Accessed: ]
rf:citation
» Fizz Buzz Interview Question | Thenjiwe kubheka | Sciencx | https://www.scien.cx/2021/08/22/fizz-buzz-interview-question/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.