Fizz Buzz solution explained
Fizz Buzz asks for the numbers 1 to n with multiples of 3 replaced by "Fizz", multiples of 5 by "Buzz" and multiples of both by "FizzBuzz". The clean solution builds a word for each number from an ordered rule list, appending "Fizz" if i % 3 == 0 and "Buzz" if i % 5 == 0, and falls back to the number when the word is empty; this avoids the classic bug of testing 3 before 15. Every version is O(n) time and O(n) for the output, so the interview is about correctness, structure and how easily the rules can change.
By Evin JohnPublished 6 min read
The problem in plain words
Count from 1 up to n. For each number, write "Fizz" if it divides evenly by 3, "Buzz" if it divides evenly by 5, "FizzBuzz" if it divides by both, and the number itself, as text, otherwise. Return the n strings in order. With n = 15 the output is 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz.
| i | Divisible by 3? | Divisible by 5? | Output |
|---|---|---|---|
| 7 | No | No | "7" |
| 9 | Yes | No | "Fizz" |
| 10 | No | Yes | "Buzz" |
| 15 | Yes | Yes | "FizzBuzz" |
| 21, with an extra rule 7 = "Bazz" | Yes | No | "FizzBazz" |
| 105, with the same extra rule | Yes | Yes | "FizzBuzzBazz" |
Why such an easy question still gets asked
Fizz Buzz is a filter. It checks, in two or three minutes, that a candidate can write a loop, use the remainder operator, order conditions correctly and return the right type. Because the algorithm is trivial, the interviewer notices everything else: naming, whether you test your output against a small case, whether you handle n = 0, and how you react when asked to add a rule. Treat it as a chance to show calm, tidy habits rather than as something to rush. There is no faster algorithm to find; all the answers below are linear, and claiming otherwise would be a red flag.
Follow-ups that turn up
- Add a rule: multiples of 7 print "Bazz". With a rule table this is a one-line change, and combined numbers like 105 come out as FizzBuzzBazz without new branches.
- Do it without the % operator. Keep countdown counters for 3 and 5 and reset them when they hit zero, as in the JavaScript version below.
- Make the rules configurable at runtime. Pass the rule list as a parameter, as the Python version does.
- Print instead of returning a list. Fine for a script, but returning values makes the function testable; say why you chose one.
- Write the tests. Check n = 0, n = 1, positions 3, 5 and 15, and the length of the output.
First version: an ordered if-chain
Most people write this first, and it is correct as long as the both-case comes before the single cases. If you test i % 3 first, 15 matches it and prints "Fizz", and the FizzBuzz branch is never reached. Testing i % 15 works because 3 and 5 share no factors, so being divisible by both is the same as being divisible by 15. The weakness shows up when the rules change: with three rules you would need branches for every combination, which grows quickly.
@@fizz-buzz/slow.py@@Expected version: build each word from a rule table
Treat each rule as a pair of divisor and word, kept in the order the words should appear. For every number, concatenate the words of the rules that divide it. If nothing matched, the word is empty and you use the number instead. Combinations fall out automatically, and the order of the table controls the order of the words, so Fizz always comes before Buzz. This is the version that shows you think about change, which is often what the interviewer was waiting for.
@@fizz-buzz/fast.py@@If the interviewer bans the modulo operator, counters do the same job. Each rule gets a countdown that starts at its divisor; every step decrements it, and when it reaches zero the word is added and the counter resets. It uses only subtraction and comparison, and it is a nice way to show you understand what divisibility means for a sequence that goes up by one.
@@fizz-buzz/fast.js@@Cost
| Approach | Time | Extra memory | Changing the rules |
|---|---|---|---|
| If-elif chain | O(n) | O(n) output | New branches for every combination |
| Rule table with concatenation | O(n * r) | O(n) output | Add one entry |
| Countdown counters | O(n * r) | O(n) output plus r counters | Add one counter |
Edge cases
- n = 0: an empty list, which every version here returns.
- n = 1: just "1".
- The first FizzBuzz at 15, which catches the order bug.
- Numbers returned as strings, not integers, if the expected output is a list of strings.
- Very large n: the output itself is the cost; if only printing is needed, a generator avoids holding all n strings at once.
Common slips
- Testing 3 or 5 before 15 in the if-chain.
- Starting the loop at 0, which prints FizzBuzz for 0, or stopping at n - 1.
- Mixing types in the result, such as integers alongside strings.
- Writing the both-case as i % 3 == 0 or i % 5 == 0 instead of and.
- Over-engineering: classes and interfaces for two rules can read as a lack of judgment. A rule list is the right amount of structure.
How to present it
Fictional example
- Candidate
- I'll loop from 1 to n. Rather than an if-chain where the order matters, I'll keep the rules as a small list, 3 to Fizz and 5 to Buzz, and join the words that apply.
- Candidate
- If no rule matches, the word is empty and I use the number. That way 15 gives FizzBuzz automatically, and adding a rule like 7 to Bazz is one line.
- Candidate
- It's linear in n. Quick check: 3 gives Fizz, 5 gives Buzz, 15 gives FizzBuzz, and n of zero gives an empty list.
Related problems
Count Primes is the next step up in divisibility questions. Fibonacci Number is another classic warm-up where the interviewer cares about the approaches you can compare. Integer to Roman also turns numbers into text with an ordered table of rules.
Natively for practice
Easy questions are where candidates lose points to nerves rather than knowledge. If you rehearse warm-ups on Mac or Windows, Natively can read the prompt from a screenshot, review your first draft for the order bug, and throw in a follow-up like a new rule or a ban on modulo. The transcript of your practice run shows whether you tested the output out loud or just declared it done.
Questions people ask
How do you solve Fizz Buzz?
Loop from 1 to n. For each number, join "Fizz" if it is divisible by 3 and "Buzz" if it is divisible by 5; if the result is empty, use the number as a string.
Why does my Fizz Buzz print Fizz for 15?
Your if-chain tests divisibility by 3 before the both-case. Test i % 15 first, or build the word by concatenation so the order of checks no longer matters.
Can you do Fizz Buzz without the modulo operator?
Yes. Keep a countdown for 3 and one for 5, decrement both each step, add the word and reset the counter when it reaches zero.
What is the time complexity of Fizz Buzz?
O(n) time, plus O(n) memory for the returned list. No version is faster, because the output itself has n entries.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
