Here is the interview question prompt, presented for reference.
Fizz Buzz is a classic interview question that apparently many experienced engineering candidates often 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, calling fizzBuzz(15) would result in '12fizz4buzzfizz78fizzbuzz11fizz1314fizzbuzz'.
The following image provides a visual of how we processed the input:
n  <= 1000n will always be a positive integer, but can be 0O(n)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 over 6 years ago by Team AlgoDaily
This is the main discussion thread generated for Fizz Buzz.
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:
      
        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.
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:
      
        Anonymous 
      
      
        Commented on
        Aug 21, 2020:
      
    one of the test failing with result - secondTest ✘ expected: but was:
The expected string is strange, it doesn't have any numbers. Clearly the expected result is wrong.
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:
      
        Anonymous 
      
      
        Commented on
        Aug 22, 2020:
      
    [deleted]
 Anonymous 
      
      
        Commented on
        Aug 23, 2020:
      
        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
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.
I'll separately look into the test expectation, not sure why the integers disappears from the expected string.
 Anonymous 
      
      
        Commented on
        Aug 24, 2020:
      
        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:
      
        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?
 Harsha Vardhan 
      
      
        Commented on
        Aug 10, 2021:
      
        Harsha Vardhan 
      
      
        Commented on
        Aug 10, 2021:
      
    Your suggested python solution is O(n) space complexity and the expected space complexity is O(1) . How can you claim it as final solution ? either change constraint on space complexity to O(n) or provide accurate solution..
 Myo 
      
      
        Commented on
        Sep 07, 2024:
      
        Myo 
      
      
        Commented on
        Sep 07, 2024:
      
    For the JS implementation, you can just use an output string without an array so that you don't have to convert the array into a string at the end.
function fizzBuzz(n) {
  let output = '';
  for (let i = 1; i