Saturday, August 29, 2009

Know Your FizzBuzz

Software engineering companies often give a prospective hire a test to see if they know how to code. There are a lot of applicants who cannot figure out or over-think how to write a simple program like FizzBuzz. Before looking at the code below, take the test yourself, see how you do! This took me about 3 minutes; where most of the time was spent setting up a project in the Eclipse IDE and typing in the source. If you are not familiar with Java, this can be easily implemented in any language. Be sure to read the article and comments for this Jeff's blog post, it is quite interesting.

CodingHorror Blog ( http://www.codinghorror.com/blog/archives/000781.html ) quotes the problem concerning the lack of competent programmers trying to obtain jobs very nicely,

"Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes. Want to know something scary? The majority of comp sci graduates can't. I've also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution." - Jeff Atwood


Now its your turn to try it! Here are the rules:

"Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"." -CodingHorror.com


public class FizzBuzz {

  
public static string fizzbuzz(int i) {
      
if(i % 3 == 0 && i %  5 == 0) {  // or i % 15 == 0
          
return "FizzBuzz";
      
}
      
else if(i % 3 == 0) {
          
return "Fizz";
      
}
      
else if(i % 5 == 0) {
          
return "Buzz";
      
}
      
else return Integer.toString(i);
  
}
  
  
public static void main(string[] args) {
      
for(int i = 1; i <= 100; i++) {
           System.out.println
(fizzbuzz(i));
      
}
   }

}

0 comments:

Post a Comment