Fizz Buzz - Problem

Given an integer n, return a string array answer (1-indexed) where:

  • answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
  • answer[i] == "Fizz" if i is divisible by 3.
  • answer[i] == "Buzz" if i is divisible by 5.
  • answer[i] == i (as a string) if none of the above conditions are true.

Input & Output

Example 1 — Basic Case
$ Input: n = 3
Output: ["1","2","Fizz"]
💡 Note: Numbers 1-3: 1 and 2 are not divisible by 3 or 5, so output as strings. 3 is divisible by 3, so output "Fizz".
Example 2 — Include Buzz
$ Input: n = 5
Output: ["1","2","Fizz","4","Buzz"]
💡 Note: Numbers 1-5: 1,2,4 output as strings. 3 divisible by 3 → "Fizz". 5 divisible by 5 → "Buzz".
Example 3 — Include FizzBuzz
$ Input: n = 15
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
💡 Note: Numbers 1-15: 15 is divisible by both 3 and 5, so output "FizzBuzz". Other multiples follow individual rules.

Constraints

  • 1 ≤ n ≤ 104

Visualization

Tap to expand
FizzBuzz Problem Overviewn = 5Input: Range from 1 to nApply Rules: Divisible by 3→"Fizz", by 5→"Buzz", by both→"FizzBuzz""1""2""Fizz""4""Buzz"1%3≠0,1%5≠02%3≠0,2%5≠03%3=04%3≠0,4%5≠05%5=0Output: ["1","2","Fizz","4","Buzz"]
Understanding the Visualization
1
Input
Integer n representing the range 1 to n
2
Process
Apply FizzBuzz rules to each number
3
Output
String array with transformed values
Key Takeaway
🎯 Key Insight: Use modulo operations to efficiently determine divisibility and apply the correct FizzBuzz transformation rule.
Asked in
Apple 25 Amazon 20 Microsoft 15 Google 10
125.0K Views
High Frequency
~10 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen