Fizz Buzz

Given an integer n, return a string array answer (1-indexed) where answer[i] is "FizzBuzz" if i is divisible by both 3 and 5, "Fizz" if i is divisible by 3, "Buzz" if i is divisible by 5, and i itself written as a string otherwise.

30:00

Fizz Buzz
easy
Free written solution
Topics
Companies

Given an integer n, return a string array answer (1-indexed) where answer[i] is "FizzBuzz" if i is divisible by both 3 and 5, "Fizz" if i is divisible by 3, "Buzz" if i is divisible by 5, and i itself written as a string otherwise.

Example 1:
Input: {"n":3}
Output: ["1","2","Fizz"]
Constraints:
  • 1≤n≤1041 \leq n \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":3}

Make an empty answer list and walk i from 1 to 3. Each i is tested against 3 and 5.

divisibility tests

÷ 3 → Fizz

i % 3

÷ 5 → Buzz

i % 5

both → FizzBuzz

i % 15

1 to 3, one 15-number cycle per row

1
2
3

Fizz ×0

Buzz ×0

FizzBuzz ×0

number ×0

answer (0 of 3)

[ ]

Variables
VariableValue
n3
answer[]
DepthFunction Call
Stack empty
0/5