What is FizzBuzz
FizzBuzz is a simple programming exercise that is frequently used as a white board problem during interviews. I, personally, have never used this problem or had anyone ask it of me during an interview so your mileage may vary.
I was inspired to write this sequence after getting Poly Bridge 2 as a Christmas gift. As I was watching some of the truly intricate bridge designs on YouTube, I also ran across a FizzBuzz video (no, I have no idea why Google put them together as a recommendation). What, I thought to myself, would happen if I took the simple FizzBuzz to the same levels as Tyler and Arglin Kampling.
I plan to discuss a progression of FizzBuzz from what I would expect from an entry level developer to an enterprise architect. Since I'm playing both sides of this, I don't expect anyone else will make the same choices as I do - what's important is the journey.
The initial solution:
package dev.boundary.waters.FizzBuzz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
/**
* A canonical FizzBuzz implementation.
*
* Write a program that prints the numbers from 1 to 100.
* But for multiples of three print “Fizz”
* instead of the number.
* For the multiples of five print “Buzz”.
* For numbers which are multiples of both three and
* five print “FizzBuzz”.
*
*/
public static void fizzBuzz() {
for(int i = 1; i < 101; i++) {
boolean isFactorThree = ((i % 3) == 0);
boolean isFactorFive = ((i % 5) == 0);
if(isFactorThree && isFactorFive) {
System.out.println("FizzBuzz");
} else if(isFactorThree) {
System.out.println("Fizz");
} else if(isFactorFive) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
}
/**
* A main method to start this application.
* The spring boot call is commented out for now.
*/
public static void main(String[] args) {
fizzBuzz();
// SpringApplication.run(MySpringBootApplication.class, args);
}
}
What's important in the solution:
- This is a basic program that has:
- Input (ranging from 1 to 100)
- Processing ( the FizzBuzz rules)
- Output
- The programmer needs understand operators in order to determine the remainder when dividing by 3 and 5.
- They also need to understand that you have to check the AND condition before the other two options.
- The input and output are currently hard coded, which in general is a poor choice.
What's less important in the solution:
- First, because I'm planning on extending this solution, all of the references to SpringBoot and Apache Camel in the project is just so I don't have to re-generate my solution for other parts of the discussion.
- Did they factor out the boolean modulus operators? As developers get more experience, they find that naming things for the intent (i.e. is it divisible by 3 or 5) is important to communicate to the reader. But on a white board where space isn't infinite, putting it in the if statement would be fine.
- All of the other things that could be put in the solution. I will discuss what I would put in, but it's going to go past what a white board could hold and I would do hand wavy lists if I were being asked what more to add.+
Comments
Post a Comment