Fizz Buzz (Easy)
Good evening! Here's our prompt for today.
You may see this problem at Doordash, Citadel, Databricks, Goldman Sachs, Twilio, Procore, Jfrog, Asana, C3 Ai, and Riot Games.
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:

Constraints
- Maximum value of Integer
n
<=1000
n
will always be a positive integer, but can be0
- 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.
xxxxxxxxxx
var assert = require('assert');
function fizzBuzz(n) {
return '';
}
try {
assert.equal(fizzBuzz(0), '');
console.log('PASSED: ' + "Expect `fizzBuzz(0)` to equal `''`");
} catch (err) {
console.log(err);
}
try {
assert.equal(fizzBuzz(1), '1');
console.log('PASSED: ' + "Expect `fizzBuzz(1)` to equal `'1'`");
} catch (err) {
console.log(err);
}
try {
assert.equal(fizzBuzz(15), '12fizz4buzzfizz78fizzbuzz11fizz1314fizzbuzz');
console.log(
'PASSED: ' +