Try it first — give it 15 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get one number n. Build a list of n strings, one for each number i from 1 to n (not from 0):
- if
idivides by both 3 and 5, write"FizzBuzz"; - else if
idivides by 3, write"Fizz"; - else if
idivides by 5, write"Buzz"; - otherwise write the number itself, as text:
"7", not7.
n is between 1 and 10⁴.
Example: n = 5 → ["1", "2", "Fizz", "4", "Buzz"].
Building the intuition
Two small ideas do all the work.
"Divides by" means "remainder is zero." The % operator gives the remainder: 9 % 3 is 0, so 9 divides by 3. 10 % 3 is 1, so 10 doesn't.
Order matters. Try writing the checks the way the rules read naturally: "if divisible by 3, Fizz; else if divisible by 5, Buzz; else if divisible by both, FizzBuzz". Now run 15 through it. 15 % 3 == 0, so it prints "Fizz" and stops. The "both" check never runs.
The fix is to put the most specific rule first. A number divisible by both 3 and 5 is divisible by 15, so check i % 15 == 0 before anything else.
The optimal approach
- Make an empty list
answer. - For
ifrom 1 ton(inclusive):- if
i % 15 == 0, add"FizzBuzz"; - else if
i % 3 == 0, add"Fizz"; - else if
i % 5 == 0, add"Buzz"; - else add
iconverted to a string.
- if
- Return
answer.
The subtle detail is the loop range. Python's range(1, n + 1) and C-style i <= n both include n. Writing range(n) gives you 0 to n − 1, which is off by one at both ends.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
answer = []
for i in range(1, n + 1): # 1 to n inclusive, not 0 to n - 1
if i % 15 == 0: # most specific rule first, or 15 would stop at "Fizz"
answer.append("FizzBuzz")
elif i % 3 == 0:
answer.append("Fizz")
elif i % 5 == 0:
answer.append("Buzz")
else:
answer.append(str(i)) # the list holds text, not numbers
return answerclass Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> answer;
for (int i = 1; i <= n; i++) { // 1 to n inclusive, not 0 to n - 1
if (i % 15 == 0) { // most specific rule first, or 15 would stop at "Fizz"
answer.push_back("FizzBuzz");
} else if (i % 3 == 0) {
answer.push_back("Fizz");
} else if (i % 5 == 0) {
answer.push_back("Buzz");
} else {
answer.push_back(to_string(i)); // the list holds text, not numbers
}
}
return answer;
}
};class Solution {
public List<String> fizzBuzz(int n) {
List<String> answer = new ArrayList<>();
for (int i = 1; i <= n; i++) { // 1 to n inclusive, not 0 to n - 1
if (i % 15 == 0) { // most specific rule first, or 15 would stop at "Fizz"
answer.add("FizzBuzz");
} else if (i % 3 == 0) {
answer.add("Fizz");
} else if (i % 5 == 0) {
answer.add("Buzz");
} else {
answer.add(String.valueOf(i)); // the list holds text, not numbers
}
}
return answer;
}
}/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function (n) {
const answer = [];
for (let i = 1; i <= n; i++) { // 1 to n inclusive, not 0 to n - 1
if (i % 15 === 0) { // most specific rule first, or 15 would stop at "Fizz"
answer.push("FizzBuzz");
} else if (i % 3 === 0) {
answer.push("Fizz");
} else if (i % 5 === 0) {
answer.push("Buzz");
} else {
answer.push(String(i)); // the list holds text, not numbers
}
}
return answer;
};Complexity
- Time: O(n). One pass, and each step is a few remainder checks.
- Space: O(1) extra. Apart from the list we must return, we only keep
i.
Edge cases and traps
- Checking 3 before 15. The classic bug: 15, 30 and 45 come out as
"Fizz". - Starting at 0.
0 % 3 == 0, so you'd add a stray"FizzBuzz"at the front. - Returning numbers instead of strings. The list holds text. Use
str(i),to_string(i),String.valueOf(i)orString(i). n = 1. The answer is just["1"].
Say it in the interview
"I loop from 1 to n and use the remainder operator to test divisibility. The only trap is order: a number divisible by both 3 and 5 would match the 3-check first, so I test divisibility by 15 before 3 and 5. It's O(n) time and constant extra space beyond the output."
Follow-ups interviewers ask
- "Now add 'Bazz' for multiples of 7." Checking every combination (15, 21, 35, 105) gets messy fast. Instead, build the string: start with
"", append"Fizz"ifi % 3 == 0,"Buzz"ifi % 5 == 0,"Bazz"ifi % 7 == 0, and use the number if it's still empty. Each new rule is one more line. - "Can you avoid the % operator?" Keep two counters that count up to 3 and 5 and reset to 0. When a counter hits its limit, that rule fires.
Practise the same idea
This is Day 1 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free