Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
For each i: if divisible by 3 AND 5 → FizzBuzz, else by 3 → Fizz, else by 5 → Buzz, else the number. Check the both-case first.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you order overlapping conditions most-specific-first.
Where candidates slip
The classic 15-before-3/5 ordering bug.
How to stand out
Offer the append-and-fallback variant that scales to more divisors.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Ordered divisibility checks
1Hint 1Explore hintHide hint
Which numbers satisfy more than one rule, and why does order matter?
2Hint 2Explore hintHide hint
Test divisible-by-15 (both) before 3 and 5.
3Hint 3Explore hintHide hint
Alternative: append 'Fizz'/'Buzz' and fall back to the number if empty.
A · Core idea
The canonical screening problem. For each i in 1..n: multiples of both 3 and 5 → "FizzBuzz", multiples of 3 → "Fizz", multiples of 5 → "Buzz", otherwise the number. The entire test is branch ordering.
Why test divisible-by-15 first. Every multiple of 15 is also a multiple of 3 and of 5. If you check % 3 before % 15, then 15, 30, 45… match the Fizz branch and never produce "FizzBuzz". So the most specific condition (both) must come first. This is the trap the problem is famous for.
The append variant (no 15 check). Build the word incrementally: append "Fizz" if divisible by 3, append "Buzz" if divisible by 5; if the word is still empty, use the number. This avoids the explicit 15 test and extends naturally to more divisors (e.g. add 7 → "Bazz").
Output type. Return strings, not ints — the number case must be stringified too.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 51,2
no divisor
"1","2"["1","2","Fizz","4","Buzz"]C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
A clean variant builds the word by appending 'Fizz' then 'Buzz' and falls back to the number if empty — no 15 check needed.
ComplexityO(n) time, O(n) space — one constant set of divisibility checks per number across n numbers.
E · Edge cases and pitfalls5n = 15 — Index 15 → "FizzBuzz".Explore edge casesHide edge cases
Edge cases handled
n = 15Index 15 → "FizzBuzz".n = 3First Fizz at 3.n = 1["1"].
Pitfalls
- Checking %3 or %5 before %15, so 15 never prints FizzBuzz.
- Returning ints instead of strings.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic java.util.List<String> fizzBuzz(int n) { java.util.List<String> out = new java.util.ArrayList<>(); for (int i = 1; i <= n; i++) { if (i % 15 == 0) out.add("FizzBuzz"); else if (i % 3 == 0) out.add("Fizz");Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Wrong branch order
Testing %3 before %15 makes every multiple of 15 print 'Fizz', never 'FizzBuzz'.
Explore the code comparisonHide the code comparison
Wrong branch order
Testing %3 before %15 makes every multiple of 15 print 'Fizz', never 'FizzBuzz'.
Don't write this
if i%3==0: 'Fizz'
elif i%5==0: 'Buzz'
elif i%15==0: 'FizzBuzz' # unreachableWrite this instead
if i%15==0: 'FizzBuzz'
elif i%3==0: 'Fizz'
elif i%5==0: 'Buzz'Revise
Carry the pattern into the interview
Pattern to remember
Ordered branch (most-specific first)
Code pattern to memorize
check %15, then %3, then %5, else numberRules to remember
- 1Test %15 before %3/%5.
- 2Else number as string.
- 3Order is the whole trick.
Use this pattern when
- Overlapping conditional rules
- Divisibility classification
Choose another pattern when
- Checking general cases before the most specific one
Interview takeaway
When conditions overlap, test the most specific one first.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Ordered branch (most-specific first). Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Fizz BuzzComing soonThe LeetCode version.