Fizz Buzz (Easy)
Good morning! 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 nwill 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.
xxxxxxxxxxdef fizz_buzz(n): return ""import unittestclass Test(unittest.TestCase): def test_1(self): assert fizz_buzz(0) == "" print("PASSED: Expect fizz_buzz(0) to equal ''") def test_2(self): assert fizz_buzz(1) == "1" print("PASSED: Expect fizz_buzz(1) to equal '1'") def test_3(self): assert fizz_buzz(15) == "12fizz4buzzfizz78fizzbuzz11fizz1314fizzbuzz" print( "PASSED: Expect fizz_buzz(15) to equal '12fizz4buzzfizz78fizzbuzz11fizz1314fizzbuzz'" )if __name__ == "__main__": unittest.main(verbosity=2) print("Nice job, 3/3 tests passed!")