Salesforce Interview Question

How would you implement a Fizzbuzz algorithm? (print each integer 1...100 but if it is a multiple of 3 print "fizz" instead, and if it is a multiple of 5 print "buzz" instead.

Interview Answers

Anonymous

Feb 21, 2017

your solution doesnt take care of when the number is both a multiple of 3 and 5, such as 15, 30 , "fizzbuzz" should be printed in this case

1

Anonymous

Feb 14, 2017

public static void fizzBuzz(){ for(int i=0; i<100; i++){ if(i%3==0){ System.out.println("Fizz"); } else if(i%5==0){ System.out.println("Buzz"); } else{ System.out.println(i); } } } Complexity O(n)