The Most Famous Coding Interview Question

FizzBuzz Challenge: One Simple Interview Question

Subscribe to my newsletter and never miss my upcoming articles

What is the most famous interview question?

FizzBuzz is the most popular coding problem, and perhaps also one of the easiest.

For those of you who don't know, here's the problem statement :

Write a short program that prints each number from 1 to 100 on a new line. For each multiple of 3, print "Fizz" instead of the number. For each multiple of 5, print "Buzz" instead of the number. For numbers that are multiples of both 3 and 5, print "FizzBuzz" instead of the number.

1_Vv4VHvLEvo9NEFxcn80Wfw.png

Seems easy right?, BUT the way you approach this reveals a lot about your programming skills

For most engineers, like myself, FizzBuzz is among the first coding problem that we solved, but why is it still used as an interview problem even in companies like Google?

Answer: Solving FizzBuzz shows interviewers much more than your programming skills

Your solution to this easy problem determines if you are just a code monkey or an experienced engineer.

According to wiki.c2.com, the “Fizz-Buzz test” is an interview question designed to help filter out the 99.5% of programming job candidates who can’t seem to program their way out of a wet paper bag.

It requires you to understand the problem, apply logic to come up with a solution, and then translate this into code.

Sometimes, even the simplest problem can become tricky. That is the case for the FizzBuzz interview question.

Solution

The idea of these simple questions is to check your programming style not to check the complexity or anything else.

Like every other coding problem, the solution for this particular problem are many, but not all are considered as good and optimal solutions.

Also don't think about complexity since you're only looping from 1 to 100

So what's the best possible answer?

Before moving any further I want you to show the possible solutions I got when I asked this question on Twitter a few months ago 🔽

All solutions I got were giving the expected result. Only difference was the way they were written

Now, Here's My approach👇

1_6oYdTqkTTk-cg3Z_Q02hIg.png

The most intuitive solution which comes to mind is this:

for var in range(1,101):
    if i%3==0 and i%5==0:
        print('FizzBuzz')
    elif i%3==0:
        print('Fizz')
    elif i%5==0:
        print('Buzz')
    else:
        print(var)

But can you find how can this be improved??

Let's suppose the interviewer says that instead of 5 print buzz when no. is divisible by 7 then you need to go & change all 5s in your code.

In a small code like this, it doesn't seem a big challenge but for a large codebase you need to take care of the changes that might come

While coding you should always think about what might change later & how will your code adapt to it

Going back and change multiple lines of code just to fit another condition is not a good practice.

So what we can do here is tweak our code a little like this :

for var in range(1,101):
    output=""
    if i%3==0:
        output+='Fizz'
    if i%5==0:
        output+='Buzz'

    print(var if output=="" else output)

OR even simpler:

for i in range(1,101):
    fizz = 'Fizz' if i%3==0 else ''
    buzz = 'Buzz' if i%5==0 else ''
    print(f'{fizz}{buzz}' or i)

In this, we are storing the final String value in a variable called output.

If the output is empty it means no condition passed above hence just print the value of var.

This looks more clean/readable & is way better for us to change if an extra condition comes.

Now even if we have to change the condition from 5 to 7 we just need to go to the single line where 5 is & alter it.

Also if another condition comes, For instance, if we add a new condition for 2 (Let's suppose when the number is multiple of 2, we will print 'Tizz' and will print 'TizzFizzBuzz' when the number is divisible by 2,3 and 5). We will just add it without affecting our code's cleanliness & readability -

for variable in range(1,101):
    output=""
   if i%2==0:  #Adding new condition
        output+='Tizz'
    if i%3==0:
        output+='Fizz'
    if i%5==0:
        output+='Buzz'

    print(variable if output=="" else output)

There's always room for improvements and we can further go into improving our code since currently there are so many IF statements and we can try to replace them with SWITCH or even use Spread Operator or Map Method(In Javascript etc.) to replace the loop altogether but that we will keep for some other day.

For now, our aim is to know that If such questions are asked in the interview just don't focus on solving for the correct answer, instead we should also try to think in terms of maintainability, testability, and safety of our code. Make sure you follow the basic programming principles like KISS, DRY, and YAGNI and apply them wherever possible.

Conclusion

Fizzbuzz isn’t a silver bullet question that determines whether the candidate is a perfect fit or not. As a side note: I have always had a mixed feeling about some coding interviews questions for filtering out candidates and how effective they really are

But as we saw, We can learn a lot about a programmer while they're approaching that simple problem:

  • Does the candidate care about clean code?
  • Does the candidate thinking about Maintainability of the code?
  • Does the candidate approaches the solution keeping Scalability factor in mind?
  • Does the candidate care about Modularity?

In case you have any questions or doubts, Let me know in the comments below. I'd be happy to answer your queries!

Thank you for reading :)


"If you enjoyed what you read & want to show some support 💙, you can do so by buying me a coffee☕"

Buy Me A Coffee

Interested in reading more such articles from Apoorv Tyagi?

Support the author by donating an amount of your choice.

Recent sponsors
GirlThatLovesToCode's photo

Great post (: I haven't think about this problem for a long time and I think I'd run to the most obvious answer even after all this time ):

Apoorv Tyagi's photo

Thanks 🙂

Initially, I think everyone goes for the most intuitive & most obvious solution which I think is OK because our first aim should be to solve the problem.

Later when you need to improve it, it is always good to think and apply the programming paradigms (especially where there are NO conditions on any type of space/time complexities) 😀

Maxi Contieri's photo

nice!

Apoorv Tyagi's photo

Thanks Maxi :)

Mr.Unity Buddy's photo

Awesome! Thank you for sharing!

Apoorv Tyagi's photo

You're welcome! Glad you liked it :)

Shreyans Jain's photo

Thanks for Sharing!

Apoorv Tyagi's photo

You're Welcome, Shreyans😊

And thanks for reading :)

Sumanshu Nankana's photo

Which website you used to use the code-snippet, it's having a 'copy' option on the top-right corner.

carbon.now.sh does not have a 'copy' option.

Apoorv Tyagi's photo

It's not from any website. I used Hashnode's Block code feature.

You can use it by wrapping the code blocks with Tripple Grave accent keys(```)

try it and let me know if I can help you further :)