DEV Community

Cover image for The Most Famous Coding Interview Question
Apoorv Tyagi
Apoorv Tyagi

Posted on • Originally published at apoorvtyagi.tech

The Most Famous Coding Interview Question

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
omicron666 profile image
Info Comment hidden by post author - thread only accessible via permalink
Omicron

apart lack of meaningful variable naming, you first solution is perfectly fine

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)

  • simple to understand
  • low memory fingerprint
  • yagni compliant
  • no multiple string contatenations of same strings

beginners : write stupid code
less beginners : write smart code
senior : deliberately write stupid code

Some comments have been hidden by the post's author - find out more