Practice Fizz Buzz
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Produce one string for each integer from 1 through n. Use FizzBuzz for multiples of both 3 and 5, Fizz for multiples of 3 only, Buzz for multiples of 5 only, and the decimal number otherwise. Keep the strings in numerical order. Practice approach: Test divisibility by 15 first, then 3 and 5. This handles the overlap without accidentally returning only Fizz or Buzz. Runtime and output space are O(n).
Source: Fizz Buzz on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
n = 3
["1","2","Fizz"]
# Example 2
n = 5
["1","2","Fizz","4","Buzz"]
# Example 3
n = 15
["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Constraints
How to solve Fizz Buzz
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
Every label depends only on divisibility by 3 and by 5, and building it by appending Fizz and then Buzz independently makes the combined case fall out automatically, which also scales cleanly to additional divisor rules.
Why brute force is not enough
There is no slower brute force here: n outputs must be produced, so O(n) is forced and 1 <= n <= 10000 is tiny. The failure mode is logical rather than performance related: returning as soon as divisibility by 3 is seen emits Fizz for 15 instead of FizzBuzz.
Approach
- Prepare an output list that will hold n strings, one per integer from 1 through n in order.
- For each integer, start with an empty label. If it is divisible by 3, append Fizz; if it is divisible by 5, append Buzz. Both appends can fire on the same integer, so multiples of 15 become FizzBuzz without a separate test.
- If the label is still empty after both checks, use the integer's own decimal string.
- Append the label and continue; the invariant is that the k-th entry is the correct label for the integer k.
Walkthrough of Example 3
n = 15 → ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
- Integers 1 and 2 are divisible by neither 3 nor 5, so the first two entries are the strings 1 and 2.
- 3 is divisible by 3 only, giving Fizz; 4 stays 4; 5 is divisible by 5 only, giving Buzz.
- 6 and 9 produce Fizz, 10 produces Buzz, and 7, 8, 11, 13, and 14 remain numeric strings.
- 12 is divisible by 3 but not by 5, so it is Fizz, not FizzBuzz.
- 15 is divisible by both, so both appends fire and the label is FizzBuzz, completing the 15-element list exactly as the expected output shows.
Time and space
Time O(n) · Space O(n). Each of the n integers is examined with a constant number of arithmetic checks, and the result list necessarily holds n strings, where n is the input bound.
Interview pitfalls
- Testing divisibility by 3 and returning immediately before testing 5, which labels 15, 30, and so on as Fizz.
- Starting the loop at 0 or ending at n - 1; the problem is one-based, and 0 is divisible by everything, which would yield a spurious FizzBuzz.
- Returning integers instead of strings for the numeric cases, which fails type checks in strongly typed judges.
- Comparing a floating-point division result to an integer rather than using the remainder operator.
Follow-up questions interviewers ask
- How would you extend this to an arbitrary list of divisor-word pairs without rewriting the conditionals?
- Can you solve it without any modulo operation, for example with counters that reset at 3 and at 5?
- If n were in the billions and the output were streamed, how would you avoid materializing the whole list?
Further reading on the technique: Fizz buzz
Related LeetCode Problems
Practice more problems similar to Fizz Buzz.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.