Interview Scare: The FizzBuzz Challenge

0
(0)

There is a famous saying:

I’m a little disheartened by the fact that 199 out of 200 applicants for programming jobs don’t know how to code. Let me repeat: they don’t know how to code. At all.

We are talking about the famous FizzBuzz problem, which was invented back in 2007.

The meaning is this:

Write a program that displays numbers from 1 to 100. Instead of multiples of 3, the program should display the word “Fizz,” and instead of multiples of 5, the program should display the word “Buzz.” If the number is a multiple of both 3 and 5, the program should display the word “FizzBuzz.”

This problem arose because even Senior Developers struggled with solving the simplest paper problems during technical interviews. Many took over 15 minutes to solve the FizzBuzz problem, although it should have taken no more than a couple of minutes.

These (and similar) tasks have now become classic interview questions – they are actively used to identify “underachievers.”

Dan Kigel described his thoughts on the matter this way:

Trust me, if you can write a loop from 1 to 10 in all the languages ​​listed on your resume, if you can solve a simple arithmetic problem in your head, and if you know how to apply recursion to a simple (but real-life) problem, then you’re already head and shoulders above most people in our industry.

So what’s the difficulty here? Why is this problem so frustrating? Let’s try solving it using Python.

Attempt 1

The first thing that comes to mind is to check the fulfillment of each condition in a loop and, depending on the result, display a number, Fizz, or Buzz.

This is what the code looks like in the first iteration:

for x in range(1, 100):
if x%3 == 0 and x%5 == 0:
print("FizzBuzz", end=' ')
elif x%3 == 0:
print("Fizz", end=' ')
elif x%5 == 0:
print("Buzz", end=' ')
else:
print(x, end=' ')

However, several recommendations can be made right away. Some are already taken into account in the code, while others simply need to be taken into account.

  • It’s better to check divisibility by 15 rather than by 3 and 5 separately. The reason is simple: one calculation is faster than two.
  • It’s better to check divisibility using the % operator rather than functions. The operator is faster.

You can also come up with a few more rules. The main thing is to understand that this isn’t just a test of “coding skills.” It also tests your ability to optimize code. The more “lean” your loop is, the better.

You’ll find a huge number of implementations of this algorithm online. Which ones are correct? It’s hard to say. Most likely, the correct code will be the one your interviewer has in mind.  So don’t try to find the “secret key” – just compare a large number of options, find the differences between them, and choose the most efficient in terms of performance and algorithm complexity.

Attempt 2

Let’s look at a few more implementations. For example, here we use strings to reduce the number of conditional blocks by 1.

for x in range(1, 100):
s = ''
if x%3 == 0:
s += 'Fizz'
if x%5 == 0:
s += 'Buzz'
if s == '':
s = x
print(s, end=' ')

Here’s another example – in one line.

[
(
"FizzBuzz" if (x%3 == 0) and (x%5 == 0)
else "Fizz" if x%3 == 0
else "Buzz" if x%5 == 0
else x
)
for x in range(1, 100)
]

In general, it’s not for nothing that they say premature optimization is the root of all evil. Trying to optimize something out of context isn’t always a smart (or useful) task. You can do something clever with data structures or something else great, but if the rest of the program uses different structures, it won’t do any good.

There is even one interesting remark on this matter:

If you think about performance, then you should generally use pre-calculated values.

For example, something like this:

print([1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz', 'Fizz', 22, 23, 'Fizz', 'Buzz', 26, 'Fizz', 28, 29, 'FizzBuzz', 31, 32, 'Fizz', 34, 'Buzz', 'Fizz', 37, 38, 'Fizz', 'Buzz', 41, 'Fizz', 43, 44, 'FizzBuzz', 46, 47, 'Fizz', 49, 'Buzz', 'Fizz', 52, 53, 'Fizz', 'Buzz', 56, 'Fizz', 58, 59, 'FizzBuzz', 61, 62, 'Fizz', 64, 'Buzz', 'Fizz', 67, 68, 'Fizz', 'Buzz', 71, 'Fizz', 73, 74, 'FizzBuzz', 76, 77, 'Fizz', 79, 'Buzz', 'Fizz', 82, 83, 'Fizz', 'Buzz', 86, 'Fizz', 88, 89, 'FizzBuzz', 91, 92, 'Fizz', 94, 'Buzz', 'Fizz', 97, 98, 'Fizz'])

Conclusion

We’ve gone over the popular FizzBuzz problem and even explored several possible solutions. It’s important to understand that the problem statement often changes slightly, so don’t get too attached to a specific formulation. It’s best to explore as many options as possible in advance so you can quickly navigate any technical interview.

And don’t be afraid to think. Write a first, rough draft of a solution, and then gradually improve it. Your interlocutor will appreciate it, I assure you. Finally, don’t forget Dan Keigel’s quote we quoted at the beginning of the article.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email

Leave a Reply

Your email address will not be published. Required fields are marked *