Category: Easy | Concepts used: Loops, modulo operator, conditional order
Problem Statement
Print numbers from 1 to 100. But:
- If divisible by
3, print"Fizz"instead of the number. - If divisible by
5, print"Buzz"instead. - If divisible by both
3and5, print"FizzBuzz".
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 ...
Examples (with edge scenarios)
| # | n | Output | Why |
|---|---|---|---|
| 1 | 3 | "Fizz" | Divisible by 3 only |
| 2 | 5 | "Buzz" | Divisible by 5 only |
| 3 | 15 | "FizzBuzz" | Divisible by both 3 and 5 |
| 4 | 7 | "7" | Not divisible by either — print the number itself |
| 5 | 1 | "1" | Smallest value in range, not divisible by anything special |
Common Fresher Mistake
Mistake What happens Fix Checking %3and%5separately with twoifblocks (notelse if) BEFORE the combined checkFor n=15, prints"Fizz"then"Buzz"separately, or misses"FizzBuzz"entirelyAlways check the combined condition ( %15==0) FIRST, before the individual%3and%5checksUsing %3==0 && %5==0as a separate late check after already returning for%3or%5"FizzBuzz"case is never reached because%3check already fired firstOrder matters — most specific condition (both) must come first
Before You Code: Clarify the Contract
Before choosing an algorithm, confirm whether zero and negative values are allowed, how large the input can be, and what should happen when an arithmetic result exceeds the chosen Java type. The examples use the contract stated in this article, but an interview answer should say these assumptions aloud.
Analogy: The Ticket Counter Validator
Imagine you are validating entry tickets (numbers) at a theater:
- Rule 1: If the ticket has a Green stamp (divisible by 3), give them a Fizz coupon.
- Rule 2: If the ticket has a Blue stamp (divisible by 5), give them a Buzz coupon.
- Rule 3: If the ticket has both Green and Blue stamps, give them a FizzBuzz VIP pass.
- If you check the Green stamp first and hand out a “Fizz” coupon immediately, you might let a VIP guest (with both stamps) walk away with just a basic “Fizz” coupon!
- To avoid this mistake, you must look for the combined stamps first (FizzBuzz) before evaluating individual stamps!
Solution 1 — if-else Chain with Combined Check First (Recommended)
Intuition
A number divisible by both 3 and 5 is also divisible by 3 alone and by 5 alone — so if you check the individual conditions first, you’ll never actually reach the “both” case. The fix: always test the most restrictive condition (divisible by both, i.e., by 15) before the looser individual ones — like checking “is this a golden ticket” before checking “is this just a regular ticket.”
public class FizzBuzz {
public static void printFizzBuzz(int limit) {
for (int i = 1; i <= limit; i++) {
if (i % 3 == 0 && i % 5 == 0) {
System.out.println("FizzBuzz"); // check BOTH first
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
}
public static void main(String[] args) {
printFizzBuzz(20); // demo with first 20 numbers
}
}
Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Dry Run (i = 15)
i % 3 == 0? 15%3=0 -> yes
i % 5 == 0? 15%5=0 -> yes
Both true -> print "FizzBuzz" (never reaches the individual Fizz/Buzz checks)
Interviewer’s take
This is the expected, safe solution. The order of conditions is the entire point of this question — interviewers use FizzBuzz specifically to see if you think through condition ordering carefully, not just whether you can write a loop.
Follow-up questions you might get:
- “Why must the combined check come first?” → Because
else ifchains stop at the firsttruematch — if%3==0is checked before%15==0, a multiple of 15 would incorrectly print just"Fizz"and never reach the FizzBuzz check. - “Can you generalize this for more divisors, like also ‘Bazz’ for divisible by 7?” → Yes — extend the combined checks and add more
else ifbranches, always ordering from most-specific (most conditions combined) to least-specific.
Solution 2 — Using String Concatenation (No Explicit “Both” Check Needed)
Intuition
Instead of manually handling the “both” case as a special condition, build up a result string piece by piece — append "Fizz" if divisible by 3, append "Buzz" if divisible by 5. If a number is divisible by both, both pieces naturally get appended together into "FizzBuzz" — no explicit combined check required!
public class FizzBuzzConcat {
public static void printFizzBuzz(int limit) {
for (int i = 1; i <= limit; i++) {
StringBuilder output = new StringBuilder();
if (i % 3 == 0) output.append("Fizz");
if (i % 5 == 0) output.append("Buzz");
System.out.println(output.length() == 0 ? String.valueOf(i) : output.toString());
}
}
public static void main(String[] args) {
printFizzBuzz(15);
}
}
Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Dry Run (i = 15)
output = ""
i%3==0 -> output.append("Fizz") -> output = "Fizz"
i%5==0 -> output.append("Buzz") -> output = "FizzBuzz"
output.length() != 0 -> print "FizzBuzz"
Interviewer’s take
This is actually a slightly more elegant solution — it avoids the “order of conditions” trap entirely by construction, and it scales beautifully if more rules are added later (e.g., “Bazz” for divisible by 7 — just add another if line). Many interviewers consider this the more “senior” answer since it shows awareness of extensibility.
📊 Visual Flowchart
graph TD
Start["Loop: i = 1 to 100"] --> CondBoth{"i % 3 == 0 AND i % 5 == 0?"}
CondBoth -->|Yes| PrintFB["Print 'FizzBuzz'"]
CondBoth -->|No| CondThree{"i % 3 == 0?"}
CondThree -->|Yes| PrintF["Print 'Fizz'"]
CondThree -->|No| CondFive{"i % 5 == 0?"}
CondFive -->|Yes| PrintB["Print 'Buzz'"]
CondFive -->|No| PrintNum["Print i"]
PrintFB --> IncLoop["i++"]
PrintF --> IncLoop
PrintB --> IncLoop
PrintNum --> IncLoop
IncLoop --> Start
Final Verdict — Which Solution Should You Give?
- Solution 1 is the standard textbook answer — correct and clearly demonstrates you understand condition ordering.
- Solution 2 is a great “next level” answer — cleaner and naturally extensible, a good one to offer if asked “can you make this cleaner/more scalable?”
Quick Recap
| Approach | Handles “both” correctly? | Extensible to more rules? | Interview Signal |
|---|---|---|---|
| if-else chain (combined check first) | Yes, if ordered correctly | Awkward — needs more else if branches | Standard, expected |
| String concatenation | Yes, naturally | Very easy to extend | Shows extra polish |
- Author
- TechByteByByte Editorial Team
- Reviewed by
- TechByteByByte Admin
- Published
- Last reviewed