Community

Filters

By Category

Youâ€™re not receiving notifications from this thread.

Fizz Buzz (Main Thread)

Here is the interview question prompt, presented for reference.

Fizz Buzz is a classic interview question that apparently many engineering candidates can't solve! Let's cover it today.

We're given a number in the form of an integer `n`.

Write a function that returns the string representation of all numbers from 1 to n based on the following rules:

• If it's a multiple of 3, represent it as "fizz".

• If it's a multiple of 5, represent it as "buzz".

• If it's a multiple of both 3 and 5, represent it as "fizzbuzz".

• If it's neither, just return the number itself.

As such, `fizzBuzz(15)` would result in `'12fizz4buzzfizz78fizzbuzz11fizz1314fizzbuzz'`.

Constraints

• Maximum value of Integer `n` <= `1000`
• `n` will always be a non zero integer (possibly `0`)
• Expected time complexity : `O(n)`
• Expected space complexity : `O(1)`

AlgoDaily partner CodeTips.co.uk has kindly provided a guide on solving Fizz Buzz in Go and Javascript. Check it out for even deeper coverage of this problem.

You can see the full challenge with visuals at this link.

Challenges â€¢ Asked almost 2 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Jul 11, 2019:

This is the main discussion thread generated for Fizz Buzz.

Jake from AlgoDaily Commented on Jul 11, 2019:

If you want to dive more into this topic, here's great article that spends WAY too much time on it: https://www.tomdalling.com/blog/software-design/fizzbuzz-in-too-much-detail/

Anonymous Commented on Aug 07, 2019:

Actually with the requirements specified as returning a string, you don't really need three test cases. Just put the i%3 test case before the i%5 test case and fizz then buzz will be added to the string resulting in fizzbuzz for the divisible by three and five test case. Use a flag to specify if either case gets triggered and if not then add the number to the string.

tomalgo Commented on Jul 09, 2020:

Why not let output be a string, then just build by concatenating with += after evaluating i? Could also demonstrate type coercion when i (integer) is coerced to string.

Anonymous Commented on Aug 21, 2020:

one of the test failing with result - secondTest âœ˜ expected:<[fizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzzfizzbuzz]fizzbuzz> but was:<[12fizz4buzzfizz78fizzbuzz11fizz1314]fizzbuzz>

The expected string is strange, it doesn't have any numbers. Clearly the expected result is wrong.

Jake from AlgoDaily Commented on Aug 21, 2020:

I can't replicate this issue. It looks like the Java version? Could you paste what you had inputted as code?

Anonymous Commented on Aug 22, 2020:

[deleted]

Anonymous Commented on Aug 23, 2020:

My code is as follows
public static String fizzBuzz(Integer num) {
StringBuffer result = new StringBuffer();
for (int n=1; n<=num; n++) {
if (num % 3 == 0 && num % 5 == 0) result.append("fizzbuzz");
else if (num % 3 == 0) result.append("fizz");
else if (num % 5 == 0) result.append("buzz");
else result.append(String.valueOf(n));
}
return result.toString();
}

Jake from AlgoDaily Commented on Aug 23, 2020:

Ah, the code is failing because your conditions are doing `num % 15` and `num % 5`, but `num` is the input integer. What you want is to use `n % 15` and `n % 5` instead.

Jake from AlgoDaily Commented on Aug 23, 2020:

I'll separately look into the test expectation, not sure why the integers disappears from the expected string.

Anonymous Commented on Aug 24, 2020:

My bad. All tests pass now. Earlier, in the failed test case the expected string was a bit strange and the real issue got overlooked. Thanks.

Ragnar Commented on Mar 23, 2021:

"Expected space complexity: O(1)"

That seems hard to achieve, won't the resulting string itself always take up O(n) space?