Programming Interviews: Fizz Buzz
When companies interview potential new software developers, they typically also have a programming interview to make sure that the candidate can actually write very simple programs. Fizz Buzz is a very simple classical example. The problem is easy to describe and the solution is easy as well.
Write a function that takes an integer and returns a string. If the number is divisible by 3, return “Fizz”. If the number is divisible by 5, return “Buzz”. If the number is divisible by 15, return “Fizz Buzz”. If the number is not divisible by 3 nor by 5, return the number itself.
In Python, a solution looks like this:
def fizzbuzz(number: int) -> str:
if number % 15 == 0:
return "Fizz Buzz"
if number % 3 == 0:
if number % 5 == 0:
Seeing which cases people test gives a good indicator of how used they are to testing at all. There are two groups of tests I want to see:
- Typical Cases
- Edge Cases
In Python, you can use Pytest to run the following tests:
from fizzbuzz import fizzbuzzdef test_3():
assert fizzbuzz(3) == "Fizz"
assert fizzbuzz(6) == "Fizz"
assert fizzbuzz(9) == "Fizz"
assert fizzbuzz(12) == "Fizz"def test_5():
assert fizzbuzz(5) == "Buzz"
assert fizzbuzz(10) == "Buzz"
assert fizzbuzz(20) == "Buzz"def test_15():
assert fizzbuzz(15) == "Fizz Buzz"
assert fizzbuzz(30) == "Fizz Buzz"
assert fizzbuzz(45) == "Fizz Buzz"def test_number():
assert fizzbuzz(1) == "1"
assert fizzbuzz(2) == "2"def test_edge_cases():
assert fizzbuzz(-1) == "-1"
assert fizzbuzz(3 ** 20) == "Fizz"
assert fizzbuzz(-(3 ** 20)) == "Fizz"
assert fizzbuzz(0) == "Fizz Buzz"
As an interviewer, I like to have follow-up questions or extensions to interview questions. Just in case the candidate is good or knows the task already.
FizzBuzz can be adjusted by changing the task from “3, 5, and 15” to “a, b, and a·b”. Or adding 11 to the mix.
A typical mistake is to check the properties in the order presented in the problem statement.
Another typical mistake is trying to get smart by having fewer lines of code, but harder logic.
What I (don’t) learn as an Interviewer
Fizz Buzz is an indicator if people have any programming skills at all. If people don’t manage to solve this relatively quickly, they cannot do anything.
You can see if they have reasonable unit test cases and how they make their code extensible. In the best case, people might use property-based testing for it:
There are a lot of important skills and factlets you will not see with this:
- Algorithms: What does the candidate know about data structures and algorithms? Do they have a good grasp of runtime and space complexity?
- Application Architecture: How well can the candidate structure code into packages and sub-packages (or modules/submodules; terminology differs in different languages)? How well can they create reasonable classes and use common design patterns? Does the candidate know how to make the application (horizontally/vertically) scalable and where the limits are?
- System Architecture: Which architectural components does the candidate have in her/his toolbelt? Which concrete product choices for the components do they know?
- Dev Workflow: Is the candidate familiar with git, Jira, Scrum, retros, sprint planning/grooming? Have they heard of branching models like git flow / Github flow / GitLab flow? How would they handle incidents and urgent requests from the outside?
- Cultural fit: What is the candidates' behavior if they don’t know something? What will they do if they made a mistake? Do they rather like to take decisions or have somebody else decide? How do they communicate with the rest of the team? How do they communicate with other stakeholders?
Not all of those topics might be relevant to your organization or the specific position.
TL;DR: Score of “Fizz Buzz” for Interviews
Overall, I think fizz buzz is too well-known to lead to any interesting results. It is trivial to solve and leaves big knowledge gaps.
Let me know if you have interview questions you would like me to explore in a similar style 🙂
Topics that might be interesting around interviewing:
- Soft questions
- Take-home exercises
- Architectural questions
There are some combinatoric/algorithmic questions for coding interviews:
- Two Sum
There are a couple of sequences based questions:
- Find a missing number in a sequence of numbers
- Find a duplicate in a sequence of numbers
- Remove duplicates from a sequence of numbers
- Rotate an array
There are also many string-based coding tasks:
- Reverse a string
- Check if two strings are palindromes