Fizz Buzz
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Description
Fizz Buzz is a classic interview question that apparently many experienced engineering candidates often can't solve! Let's cover it today.
We're given a number in the form of an integer n.
Write a function that returns the string representation of all numbers from 1 to n based on the following rules:
If it's a multiple of 3, represent it as "fizz".
If it's a multiple of 5, represent it as "buzz".
If it's a multiple of both 3 and 5, represent it as "fizzbuzz".
If it's neither, just return the number itself.
As such, calling fizzBuzz(15) would result in '12fizz4buzzfizz78fizzbuzz11fizz1314fizzbuzz'.
The following image provides a visual of how we processed the input:

Constraints
- Maximum value of Integer
n<=1000 -
nwill always be a positive integer, but can be0 - Expected time complexity :
O(n) - Expected space complexity :
O(1)
AlgoDaily partner CodeTips.co.uk has kindly provided a guide on solving Fizz Buzz in Go and Javascript. Check it out for even deeper coverage of this problem.
```js
function fizzBuzz(n) {
return '';
};
```
```java
public String fizzBuzz(int n) {
return "";
}
```
```python
def fizzBuzz(n):
return ""
```
```cpp
std::string fizzBuzz(int n) {
return "";
}
```
```csharp
public string FizzBuzz(int n) {
return "";
}
```
```go
func fizzBuzz(n int) string {
return ""
}
```
Brute Force
Translating Decision Flows to Control Statements
Getting your dream software engineering job often involves cracking coding interviews, and the "FizzBuzz" problem is a classic example that tests your grasp of basic control flow. Let's delve into how to map the problem's requirements to technical implementations.
The Problem Statement Translated
The problem tasks you with making the following decisions based on the input number:
- Multiple of 3: Return "fizz"
- Multiple of 5: Return "buzz"
- Multiple of both 3 and 5: Return "fizzbuzz"
- None of the above: Return the number itself
Why Use Control Statements?
You might be wondering why control statements like if-else and switch are the way to go here. That's because these statements allow us to simulate decision-making in code, just as we do in everyday life. When your language doesn't include a switch statement, if-else ladders are the tool of choice.
The Art of Decision Making in Code
Step 1: Identify the Key Decisions
First, observe the series of decisions needed. Are they mutually exclusive? In our case, yes—each number can only map to one output. This screams "use an if-else ladder."
Step 2: Translate to if-else
Translate each decision point to an if-else statement.
- If a number is a multiple of both 3 and 5, the number is a multiple of 15 (
3 * 5). - To check if a number, say
n, is a multiple of another number, saym, we use the modulo operator%. Ifn % m == 0, thennis a multiple ofm.
if ( // multiple of 3 ) {
// fizz
} else if ( // multiple of 5 ) {
// buzz
} else if ( // multiple of 3 and 5 ) {
// fizzbuzz
} else {
// return number
}
if # multiple of 3:
# fizz
elif # multiple of 5:
# buzz
elif # multiple of 3 and 5:
# fizzbuzz
else:
# return number
if (/* multiple of 3 */) {
// fizz
} else if (/* multiple of 5 */) {
// buzz
} else if (/* multiple of 3 and 5 */) {
// fizzbuzz
} else {
// return number
}
if /* multiple of 3 */:
# fizz
elif /* multiple of 5 */:
# buzz
elif /* multiple of 3 and 5 */:
# fizzbuzz
else:
# return number
if (/* multiple of 3 */) {
// fizz
} else if (/* multiple of 5 */) {
// buzz
} else if (/* multiple of 3 and 5 */) {
// fizzbuzz
} else {
// return number
}
if (/* multiple of 3 */) {
// fizz
} else if (/* multiple of 5 */) {
// buzz
} else if (/* multiple of 3 and 5 */) {
// fizzbuzz
} else {
// return number
}
if /* multiple of 3 */ {
// fizz
} else if /* multiple of 5 */ {
// buzz
} else if /* multiple of 3 and 5 */ {
// fizzbuzz
} else {
// return number
}
Right Operator
Unveiling the Magic of the Modulo Operator
Just like you would use division in everyday math to find out if one number evenly fits into another, you can do the same in programming. However, there's a more elegant solution at hand: the modulo operator %.
A Visual Cue to Modulo Operation
Imagine the number 6 as a full container of water, and the number 3 as an empty cup. If you can pour the water into the empty cup such that the container becomes completely empty with no overflow or leftover, then it's safe to say 3 "neatly divides" into 6.

Why Division Falls Short
While division (/) can be used, it creates a mess. You'd need to deal with quotients and additional logic to make sense of any leftovers. This is like trying to use a measuring cylinder to manually calculate if the water can completely fill the empty cup, drop by drop.
Cumbersome, right?
The Power of Modulo (%)
Enter the modulo operator (%). This tool directly gives you any leftovers (the remainder) when you try to fit one number into another. If it gives you a zero, voila! The numbers fit perfectly.
Here's a quick example:
6 % 3
// 0
// 3 neatly divides into 6
The Practicality of Modulo in the FizzBuzz Problem
For FizzBuzz, using % is an absolute win. Just check if the number, when divided by another (say 3 or 5), leaves a remainder of zero:
- If
n % 3 == 0, it's "fizz." - If
n % 5 == 0, it's "buzz." - If
n % 15 == 0, it's "fizzbuzz."
See? No mess, no fuss. Just a neat, simple solution for identifying multiples. And this, my friends, is why the modulo operator is your best companion for problems involving division and remainders.
Conditional Flow
Navigating the Maze of Conditionals: Order Matters!
Here's the classic pitfall.
The order of conditionals in FizzBuzz is an often overlooked detail that makes all the difference. Let's dive into understanding why the sequence is important, and then we can talk a bit about performance.
The Conditional Trap
Remember the earlier example where we used the following if-else ladder in various languages?
if ( // multiple of 3 ) {
// fizz
} else if ( // multiple of 5 ) {
// buzz
} else if ( // multiple of 3 and 5 ) {
// fizzbuzz
} else {
// return number
}
if # multiple of 3:
# fizz
elif # multiple of 5:
# buzz
elif # multiple of 3 and 5:
# fizzbuzz
else:
# return number
if (num % 3 == 0) {
// fizz
} else if (num % 5 == 0) {
// buzz
} else if (num % 3 == 0 && num % 5 == 0) {
// fizzbuzz
} else {
// return number
}
if (num % 3 == 0) {
// fizz
} else if (num % 5 == 0) {
// buzz
} else if (num % 3 == 0 && num % 5 == 0) {
// fizzbuzz
} else {
// return number
}
if (num % 3 == 0) {
// fizz
} else if (num % 5 == 0) {
// buzz
} else if (num % 3 == 0 && num % 5 == 0) {
// fizzbuzz
} else {
// return number
}
if num % 3 == 0 {
// fizz
} else if num % 5 == 0 {
// buzz
} else if num % 3 == 0 && num % 5 == 0 {
// fizzbuzz
} else {
// return number
}
This isn't quite right!
Here's where many people also go wrong. Why? Because the order will short-circuit the fizzbuzz condition, which needs both 3 and 5 as divisors.
In our conditional, we'll want to address the fizzbuzz scenario first, since it needs to go before the fizz and buzz blocks. Because it is requires being divisible by both 3 and 5, it will get short circuited by either the 3 OR 5 cases first.
To restate, I mean that we need it to pass through the i % 3 == 0 && i % 5 == 0 condition before either of the individual % 3 or % 5 ones. Those cases are still true (and their logic will execute) even in cases where it's true for i % 3 && i % 5.
The time complexity is O(n) as it involves a single loop that iterates through each element once. When you're in a coding interview, pointing out the time complexity can score you extra points.
Some more helpful links: - https://www.codetips.co.uk/fizz-buzz/ - https://ditam.github.io/posts/fizzbuzz/
One Pager Cheat Sheet
One Pager Cheat Sheet
- We are given an integer
n, and asked to write a functionfizzBuzz()that returns the string representation of all numbers from1tonusing the rules "fizz", "buzz" and "fizzbuzz" for multiples of 3, 5 and both respectively, with a maximum value of1000,O(n)time complexity andO(1)space complexity. - The key to answering this technical interview question was to identify how to map a problem to its technical implementation, such as using
if-elseorswitchcontrol flows. - We can use the
modulo operator(%) to easily determine if one number is a multiple of another - the remainder returned will be0if it is. - We can use a conditional flow order of
fizzbuzz>fizz>buzz>return numberto check if a number is divisible by either3,5, both, or neither, with a time complexity ofO(n).