dsa · easy
Fizz Buzz
For the integers 1 through n inclusive, build a list of strings of length n using these rules, in this priority:
- if the value is divisible by both 3 and 5, write
FizzBuzz - else if it is divisible by 3, write
Fizz - else if it is divisible by 5, write
Buzz - else write the decimal digits of the value itself
Index 0 of the list is the rule for 1, index 1 is the rule for 2, and so on.
Example
n = 5:
- 1 →
"1" - 2 →
"2" - 3 is divisible by 3 →
"Fizz" - 4 →
"4" - 5 is divisible by 5 →
"Buzz"
Answer: ["1","2","Fizz","4","Buzz"].
n = 15 also hits 15, which is divisible by both 3 and 5, so that slot is "FizzBuzz" (not "Fizz" or "Buzz" alone).
## Arguments - n — inclusive upper integer; the list has length n
Constraints
1 <= n <= 4*10^4 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: 5 Expected: ["1","2","Fizz","4","Buzz"]
Example 2
Input: 1 Expected: ["1"]
Example 3
Input: 15 Expected: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]