Fizz Buzz
The famous screening question. Print 1…n, but multiples of 3 become "Fizz", of 5 become "Buzz", and of both become "FizzBuzz". Easy — the catch is testing divisibility in the right order.
The problem
Return a list of length n where, for each i from 1 to n: it is "FizzBuzz" if i is divisible by both 3 and 5, "Fizz" if only by 3, "Buzz" if only by 5, otherwise the number as a string.
n = 5["1", "2", "Fizz", "4", "Buzz"]n = 15[..., "13", "14", "FizzBuzz"]n = 3["1", "2", "Fizz"]- 1 ≤ n ≤ 10⁴
- Every element is a string — including the plain numbers.
- Check divisibility by 15 first (or combine the two booleans), or the FizzBuzz case never fires.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement fizzbuzz(n): return the list of length n following the Fizz / Buzz / FizzBuzz rules. Watch the order — test "both" before either single case.
- Loop
ifrom 1 to n inclusive. - Divisible by both 3 and 5 is the same as divisible by 15 — check that first.
- Otherwise check 3, then 5, then fall back to the number.
- Everything in the list is a string, so convert the plain numbers too.
Approach, complexity & discussion — open after you solve
The approach
For each number, print “Fizz” if it is divisible by 3, “Buzz” if divisible by 5, “FizzBuzz” if by both, otherwise the number itself. The clean way is to check the both case first (divisible by 15), or to build the string from two independent checks and fall back to the number only if the string is still empty.
Complexity
Time O(n).
Common mistakes
- Checking 3 and 5 before 15, so multiples of 15 print “Fizz” instead of “FizzBuzz”.
- Off-by-one on the range (starting at 0, or missing the last number).
- Over-engineering a five-line problem.
Where this shows up
FizzBuzz is the classic screening warm-up — it filters for basic control flow and, subtly, for handling the combined case in the right order. Trivial as it is, the divisibility-ordering trap is a real one that catches rushed candidates.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.