Interview Scare: The FizzBuzz Challenge
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.
Explore More IT Terms
#
A
- A Guide to SQL Query Formatting
- A/B testing
- Agile
- Algorithm complexity in 5 minutes
- Algorithms and Data Structures in C#
- An overview of the C # programming language
- An overview of the Python programming language
- Anaconda Python
- Android
- Android App Bundle
- Android SDK
- Angular
- Ansible
- Apache
- Apache Airflow
- Apache Kafka
- Apache Tomcat
- App Store
- AppCode
- Array-based stack
- ArrayList
- ASCII
- ASP.NET
- Assembly Language Lessons
B
C
D
- Data Analytics: applications of data analysis in companies
- Data Engineer - Who is it, what does a data engineer do, and an overview of the profession
- Data modeling: what it is, types, and process steps.
- Data preprocessing: a complete guide for beginners and professionals.
- Data structure
- Data structures
- Defining Aliases
- Defining Arrays
- Deque
- Developing a Website from Scratch
- Digital data: understand the importance of this asset for businesses.
- Doubly linked lists
E
F
H
- Handling errors and exceptions
- How to effectively organize your workflow
- How to Learn Java: Tips for Beginner Developers
- How to Learn PHP: A Beginner's Guide
- How to Use S3 Storage in Kubernetes with CSI
- HTML
- HTML and CSS: Definition, Application, and Operating Principles
- HTML and CSS. Layout from Scratch: What to Learn, Where to Learn, and How Long Will It Take?
- HTML Frame Structure
- HTML Link Formatting
I
- if..else construction
- Inserting an Image
- Interactive Python Tutorial – Learn Programming from Scratch
- Interview Problem: Finding a Deleted Element in O(N)
- Interview Scare: The FizzBuzz Challenge
- Introduction to C++
- Introduction to Machine Learning
- Introduction to programming languages
- IT Specialist Resume (CV)
K
M
O
P
S
- SFML Graphics Library Tutorials
- SQL commands: see what they are, what the main ones are + examples
- SQL Interview Questions and Tasks
- SQL Lessons
- SQL Stored Procedures
- SQL Syntactic Sugar: The COALESCE Function
- Stack
- Start in analytics: Python or R
- Statistical analysis: importance for decision making.
- String formatting in Python
- Swift Lessons
- switch/match construct
T
W
- What are databases, and why do they need DBMS and SQL?
- What do Linux distributions consist of?
- What is .NET and what is it used for?
- What is a GPU in a computer, in simple terms?
- What is Big Data? Introduction, Types, Characteristics, and Examples
- What is Golang and what is it used for?
- What is Haskell and what is it used for?
- What is Kotlin and what is it used for?
- What is Linux? The History of Linux
- What is machine learning, and how does it work?
- What is Power BI: everything about the data analytics software
- What is the C++ programming language?
- What is the OSI Model: A Complete Explanation of the Seven Layers and Their Role in Networking
- Where to start learning the C programming language?
- Which Linux distribution should you choose? A Linux distribution overview
