FizzBuzz Java | Fresher Interview
Fast recall
Quick revision
Core answer
Whether you handle the both-multiples case correctly and avoid redundant divisibility checks
Checking 3 and 5 before the combined 15 case, so 15 prints 'Fizz' instead of 'FizzBuzz'
Use the string-append approach to avoid the ordering bug and keep it extensible
Interview-ready guide
Interview answer
FizzBuzz is a classic warm-up.
I print the numbers from one to n, but for multiples of three I print Fizz, for multiples of five I print Buzz, and for multiples of both, which is multiples of fifteen, I print FizzBuzz. The important detail is handling the both-multiples case correctly, because there is a subtle ordering trap.
If I write the if-else with the modulo-three check first, then fifteen satisfies it and prints Fizz, and it never reaches the FizzBuzz case. So in the if-else style, I check the combined case first, either modulo fifteen equals zero or divisible by both three and five, then check three, then five, then fall back to printing the number.
A clean alternative is to build the output string from independent rules. I start with an empty string, append Fizz if the number is divisible by three, append Buzz if it's divisible by five, and then at the end, if the string is still empty I print the number, otherwise I print the string.
This naturally handles fifteen, because both appends fire and I get FizzBuzz, and there's no ordering to get wrong. It's also more extensible: if someone adds a new rule, like print Bazz for multiples of seven, I just add one more append line instead of rewriting a tangle of nested conditionals. It is O of n, and the rule-building version stays easy to extend without a tangle of conditionals.
FizzBuzz Java | Fresher Interview
Direct Answer
FizzBuzz: print 1..n, but print Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both (i.e. 15). The trap is order: check 15 (both) first, then 3, then 5. Even cleaner is to build a string by appending 'Fizz' if divisible by 3 and 'Buzz' if by 5, printing the number only if the string is empty.
Deep Dive & Architecture
FizzBuzz prints one result for each number from 1 through n: Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for multiples of both, and the number otherwise.
The direct conditional version must test divisibility by both numbers before testing either one alone. At 15, both 15 % 3 == 0 and 15 % 5 == 0 are true. If the multiple-of-3 branch comes first in an if/else if chain, it prints only Fizz and the later combined case is never reached.
Worked example: 13 through 16
- 13 matches no rule, so print
13. - 14 matches no rule, so print
14. - 15 matches both, so print
FizzBuzz. - 16 matches no rule, so print
16.
Another clean version builds a string: append Fizz when divisible by 3 and append Buzz when divisible by 5. If the string is empty, print the number. This version naturally combines rules and is easier to extend with another divisor.
The program performs constant work for each of n numbers, so it takes O(n) time. Apart from output, it uses O(1) extra space. Handle n <= 0 according to the method contract, usually by producing no output.
Common Mistakes & Pitfalls
- Wrong order: checking
% 3before the combined case makes 15 printFizz. - Redundant checks:
% 3 && % 5is the same as% 15- either is fine, just be consistent. - Off-by-one: loop must be
1..ninclusive. - Hard to extend: nested if-else gets messy if rules grow (7 -> 'Bazz'); the string approach scales.
Check your understanding
Answer these without looking back at the article.
- How do you check if a number is prime in Java?
- How do you find the second largest element in an array?
- How do you rotate an array by k positions in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.