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
<= 1000
n
will always be a positive integer, but can be 0
O(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 5 years ago by Jake from 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/
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.
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.
I can't replicate this issue. It looks like the Java version? Could you paste what you had inputted as code?
[deleted]
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();
}
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.
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.
"Expected space complexity: O(1)"
That seems hard to achieve, won't the resulting string itself always take up O(n) space?
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..
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 <= n; i++) {
if (i % 3 === 0 && i % 5 === 0) {
output += 'fizzbuzz';
} else if (i % 3 === 0) {
output += 'fizz';
} else if (i % 5 === 0) {
output += 'buzz';
} else {
output += i.toString();
}
}
return output;
}