4

The FizzBuzz question is a very classical interview question asked in multiple interviews all around the world. There are many ways to do it in different languages. But in most general terms it definitely involves using 3 or 4 if/else if loops.There are fancy ways to do it if we go into Java 8 territory and such. But my main question is this:

  • What is the logic behind this famous question?
  • What are the interviewers looking for in the candidate when they ask this question?
  • Do we know how to code basic stuff?
  • Do they want to see our style of coding?
  • Are they expecting optimization in this question? It's hard to try and optimize the code.

Here's a mathematical take on it:

if Loop count = 100

%15 calculation = 100

%3 calculation = 100 - 6 = 94

%5 calculation = 100 - 33 = 67

Total modulus calculation = 261 (100 + 94 + 67)

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
shd293
  • 97
  • 1
  • 8

2 Answers2

2

What is the logic behind this famous question?

It's genuinely to weed out people that have no idea what they are doing.

What are the interviewers looking for in the candidate when they ask this question?

Most importantly for me its a conversation starter. You can ask people to explain their code why they choose x % 15 == 0 over x % 3 == 0 && x % 5 == 0 for example.

Do we know how to code basic stuff?

Yes, conditions; loops; operators

Do they want to see our style of coding?

Yes, this may be about optimization or just conversation. The interview process is about understanding the individual in front of you. (and that goes both ways).

Are they expecting optimization in this question?

That depends, I think there's a combination of if elseif and different ways you can loop. Typically, each interviewer should put their own spin on the question. For example in C# I would set my test up to include an element on memory management with a yield return expectation.

You may want to read Jeff Atwood's post on the topic.

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
  • Thank you for the answer. I would like re-ask this since you made the point about %15, Is using "%(5*3)" or "%3 && %5" instead of the former the way to go ? – shd293 Feb 08 '18 at 10:45
  • 1
    @shd293 they are just different, in an interview I would just ask why you choose to do it 1 way as opposed to the other. There isn't really a right or wrong answer. You could say that `%15` is more efficient as you are only checking one condition. – Ashley Medway Feb 08 '18 at 10:47
1

Recently I was asked this question in interview, I didn't spend much time thinking , just wrote a simple java method which take integer and returns appropriate Fizz Buzz word, and I asked the question to interviewer, what is the expected the change in the program, and he said another condition like number divisible by 7 print 'SEVEN' so I refactored a bit and final answer was something like below

 public static void main(String[] args) {

    Function<Integer,String> fizz = e->e%3==0?"FIZZ":"";
    Function<Integer,String> Buzz = e->e%5==0?"BUZZ":"";
    Function<Integer,String> fizzBuzz = e->e%3==0 && e% 5==0?"FIZZBUZZ":"";
    Function<Integer,String> seven = e->e%7==0?"SEVEN":"";
    List<Function<Integer, String>> predicateList = List.of(fizz,Buzz,fizzBuzz,seven);
    IntStream.range(0,100).boxed()
                        .map(e->applyFizzBuzzFunc(e,predicateList))
                        .forEach(System.out::println);
}

/**
 * Executes the list in the given order 
 */
private static String applyFizzBuzzFunc(Integer e, List<Function<Integer, String>> predicateList) {
    return predicateList.stream()
                        .map(f -> f.apply(e))
                        .filter(s -> !"".equals(s))
                        .findFirst()
                        .orElse(String.valueOf(e));
}

I could see that interviewer was quite impressed with the solution. In my opinion be as much as interactive with interviewer and try to understand what exactly he is trying check.

Rahul B
  • 132
  • 1
  • 1
  • 11
  • This is a cool way to this , thank you for the solution :D In java 8 , I have learnt that it can be done in 4-5 lines. – shd293 Jun 23 '18 at 05:33