patternjavaMajor
Which FizzBuzz is better, and why?
Viewed 0 times
whyandbetterfizzbuzzwhich
Problem
I was in an interview and the guy asked me to do the typical FizzBuzz question of printing numbers 1-100, but for each factor of 3 print Fizz, each factor of 5 print Buzz, and each factor of both print FizzBuzz. I wrote the following:
The interviewer had me change my answer so that it looked like this:
Then he asked me which I would prefer if I was handed that code, and said there was no wrong answer (which I should have known was a lie), but I foolishly said mine because it was easier for me to look and see that something different was happening for factors of 15 in my code by just glancing over it, whereas without actually looping through the code in my mind, I wouldn't know that about the second version.
Anyways, my question is, is his version better for some reason that I am missing? I'm at entry level skill, so I honestly think that both versions are completely identical efficiency and readability wise.
public static void fizzbuzz(){
for(int i=1;i<=100;i++){
if(i%15 == 0){
System.out.println("FizzBuzz");
} else if (i%3 == 0) {
System.out.println("Fizz");
} else if (i%5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
}The interviewer had me change my answer so that it looked like this:
public static void fizzbuzz(){
for(int i=1;i<=100;i++){
String str = "";
if(i%3 == 0){
str = "Fizz";
}
if(i%5 == 0){
str += "Buzz";
}
if(str.equals("")){
str = i;
}
System.out.println(str);
}
}Then he asked me which I would prefer if I was handed that code, and said there was no wrong answer (which I should have known was a lie), but I foolishly said mine because it was easier for me to look and see that something different was happening for factors of 15 in my code by just glancing over it, whereas without actually looping through the code in my mind, I wouldn't know that about the second version.
Anyways, my question is, is his version better for some reason that I am missing? I'm at entry level skill, so I honestly think that both versions are completely identical efficiency and readability wise.
Solution
I prefer your solution to his. My reasoning is that:
Both options could be better though. The lack of 'breathing space' (white space between keywords, variables, values, and operators) leads to hard-to-read code. Lines like:
should be:
Then, your solution would actually be more efficient as a switch statement:
The switch statement will be optimized so that there does not need to be more than one modulo, and there is no concept of an
- yours has constants only in the String manipulation
- you do not do string concatenation
- the logic is distinct for each remainder.
Both options could be better though. The lack of 'breathing space' (white space between keywords, variables, values, and operators) leads to hard-to-read code. Lines like:
for(int i=1;i<=100;i++){should be:
for (int i = 1; i <= 100; i++) {Then, your solution would actually be more efficient as a switch statement:
for (int i = 1; i <= 100; i++) {
switch (i % 15) {
case 0:
System.out.println("FizzBuzz");
break;
case 3:
case 6:
case 9:
case 12:
System.out.println("Fizz");
break;
case 5:
case 10:
System.out.println("Buzz");
break;
default:
System.out.println(i);
}
}The switch statement will be optimized so that there does not need to be more than one modulo, and there is no concept of an
if/elseif/elsif/else... just a single condition check.Code Snippets
for(int i=1;i<=100;i++){for (int i = 1; i <= 100; i++) {for (int i = 1; i <= 100; i++) {
switch (i % 15) {
case 0:
System.out.println("FizzBuzz");
break;
case 3:
case 6:
case 9:
case 12:
System.out.println("Fizz");
break;
case 5:
case 10:
System.out.println("Buzz");
break;
default:
System.out.println(i);
}
}Context
StackExchange Code Review Q#60145, answer score: 32
Revisions (0)
No revisions yet.