Skip to content
Advertisement

FizzBuzz using limited number of conditions and StringBuilder

All of you know the trivial fizzbuzz question during the first junior interviews. In my case, it was more than write a solution. There were more requirements:

  1. Only two ifelse statements.
  2. StringBuilder required.
  3. No Map, Collection.
  4. No ternary operator.
  5. Only Java core (8 or 11).
  6. You have only 5 minutes (in my case, but for you it doesn’t matter).

My solution with three if statements:

JavaScript

But yeah, there are three ‘if’ and no StringBuilder. Okay, let’s see two ‘if’ example:

JavaScript

And again it’s wrong: two ‘if’ but no StringBuilder.

Can I ask for a favor? Help me solve this problem.

Advertisement

Answer

I’d say your solution with the map was very close, and nobody said “no arrays” 🙂

JavaScript

Which prints

JavaScript

Having said that, I think 5 minutes is a bit too strict for this kind of problem.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement