Fizz Buzz
Easy
Topics
Given an integer n, return a string array answer of length n where, for each i from 1 to n:
answer[i-1] == "FizzBuzz"ifiis divisible by both 3 and 5;answer[i-1] == "Fizz"ifiis divisible by 3;answer[i-1] == "Buzz"ifiis divisible by 5;answer[i-1] == ias a string otherwise.
Example 1
divisible by 3
One chip per value from 1 to 3. A value divisible by both 3 and 5 is labelled
FizzBuzz rather than one or the other, which is the whole content of the problem. Everything else keeps its own number, as a string.Each chip is one entry of the answer array.
Input: n = 3 Output: ["1","2","Fizz"]
Example 2
divisible by 3divisible by 5
Input: n = 5 Output: ["1","2","Fizz","4","Buzz"]
Example 3
divisible by 3divisible by 5divisible by both
Input: n = 15 Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Constraints
- 1 <= n <= 10^4
Editorial — intuition, approach, complexity and pitfalls
Intuition
The only thing to get right is the order of the tests. Divisible by 15 has to be checked before divisible by 3 or 5, because 15 satisfies all three and only the combined label is correct.
Writing it as "divisible by 3 and by 5" rather than "divisible by 15" keeps the rule readable and generalises if a third divisor is ever added.
Approach
- Walk
ifrom 1 ton. - Build the label by concatenation: start empty, append
"Fizz"wheni % 3 == 0, then append"Buzz"wheni % 5 == 0. - If the label is still empty, use the decimal form of
i. - That ordering produces
"FizzBuzz"for multiples of 15 with no separate case.
Walkthrough
n = 5
i=1 3? no 5? no -> "1"
i=2 3? no 5? no -> "2"
i=3 3? yes 5? no -> "Fizz"
i=4 3? no 5? no -> "4"
i=5 3? no 5? yes -> "Buzz"Complexity
- Time — O(n): one label per value, each built in constant time.
- Space — O(n): the output array; nothing else is retained.
Common Pitfalls
- Checking
i % 3first and returning immediately mislabels every multiple of 15. - The values are strings, including the plain numbers — a mixed array of numbers and strings is not the required shape.
Full Solution — all languages
class Solution:
def fizzBuzz(self, n):
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 outimport java.util.*;
class Solution {
public List<String> fizzBuzz(int n) {
List<String> out = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i % 15 == 0) out.add("FizzBuzz");
else if (i % 3 == 0) out.add("Fizz");
else if (i % 5 == 0) out.add("Buzz");
else out.add(String.valueOf(i));
}
return out;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: 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;
}
};/**
* @param {number} n
* @return {string[]}
*/ 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;
};