Fizz Buzz — Solution in Python, Java & C++
Fizz Buzz solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings and math.
Fizz Buzz is an easy coding problem on strings, math and simulation, asked in Capital One and Spotify interviews.
- Difficulty: Easy
- Topics: Strings, Math, Simulation
- Asked at: Capital One, Spotify
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an integer n, return a string array answer (1-indexed) where:
answer[i] == "FizzBuzz"ifiis divisible by 3 and 5.answer[i] == "Fizz"ifiis divisible by 3.answer[i] == "Buzz"ifiis divisible by 5.answer[i] == i(as a string) otherwise.
Example 1
Input: n = 3
Output: ["1","2","Fizz"]
Example 2
Input: n = 5
Output: ["1","2","Fizz","4","Buzz"]
Example 3
Input: n = 15
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Constraints
1 <= n <= 10^4
Hints for Fizz Buzz
Hint 1
Check divisibility by 15 first (or by both 3 and 5) — otherwise "Fizz" or "Buzz" will shadow "FizzBuzz".
Hint 2
Building the string by concatenating "Fizz" then "Buzz" when each divides avoids the 15 case entirely.
How to solve Fizz Buzz
Walk 1…n and classify each value, testing the combined case first so a multiple of 15 is not captured by the Fizz branch.
Approach
- For each
ifrom 1 ton, checki % 15 == 0→"FizzBuzz". - Else
i % 3 == 0→"Fizz", elsei % 5 == 0→"Buzz". - Otherwise append the decimal string of
i.
Why it works
Divisibility by both 3 and 5 is equivalent to divisibility by 15 because they are coprime, and ordering the branches most-specific first guarantees each i lands in exactly one.
Complexity
- Time —
O(n) - Space —
O(n) for the output
Pitfalls
- Testing
% 3before% 15silently turns everyFizzBuzzintoFizz. - The array is 1-indexed in the statement; the loop must start at 1, not 0.
Reference solution
Python
def fizzBuzz(n: int):
out = []
for i in range(1, n + 1):
if i % 15 == 0:
out.append("FizzBuzz")
elif i % 3 == 0:
out.append("Fizz")
elif i % 5 == 0:
out.append("Buzz")
else:
out.append(str(i))
return outJava
public static String[] fizzBuzz(int n) {
String[] out = new String[n];
for (int i = 1; i <= n; i++) {
if (i % 15 == 0) out[i - 1] = "FizzBuzz";
else if (i % 3 == 0) out[i - 1] = "Fizz";
else if (i % 5 == 0) out[i - 1] = "Buzz";
else out[i - 1] = String.valueOf(i);
}
return out;
}C++
vector<string> fizzBuzz(int n) {
vector<string> out;
for (int i = 1; i <= n; i++) {
if (i % 15 == 0) out.push_back("FizzBuzz");
else if (i % 3 == 0) out.push_back("Fizz");
else if (i % 5 == 0) out.push_back("Buzz");
else out.push_back(to_string(i));
}
return out;
}JavaScript
var fizzBuzz = function(n) {
const out = [];
for (let i = 1; i <= n; i++) {
if (i % 15 === 0) out.push("FizzBuzz");
else if (i % 3 === 0) out.push("Fizz");
else if (i % 5 === 0) out.push("Buzz");
else out.push(String(i));
}
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings