Fizz Buzz
Maintained by Vikas D, software engineer
You are given a single positive integer n. Walk through the whole numbers from 1 up to and including n and decide what each one becomes in the output.
The rule depends on divisibility. A number divisible by 3 turns into "Fizz", a number divisible by 5 turns into "Buzz", and a number divisible by both 3 and 5 turns into "FizzBuzz". Any number that is divisible by neither stays as its own value, written as a string.
Return the results in order, so position i of the output corresponds to the number i (counting from 1). The output always has exactly n entries, and every entry is a string.
Examples
1 and 2 are plain numbers, 3 is a multiple of 3 so it becomes Fizz, 4 is plain, and 5 is a multiple of 5 so it becomes Buzz.
15 is the first number divisible by both 3 and 5, so it becomes FizzBuzz. Every third slot is Fizz and every fifth slot is Buzz, except where they overlap.
1 is divisible by neither 3 nor 5, so it stays as the string "1".
Constraints
- 1 <= n <= 10^4
- Every element of the returned list is a string, including the plain numbers.
- The list is 1-indexed: position i holds the answer for the integer i.
Visible test cases
- fizzBuzz(5) == ["1","2","Fizz","4","Buzz"]
- fizzBuzz(15) == ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Hints
Hint 1
Loop from 1 to n and handle one number at a time. The hard part is only choosing which of the four outputs each number gets.
Hint 2
Order your checks carefully. Test divisibility by both 3 and 5 first, otherwise a multiple of 15 would match the Fizz branch and never reach FizzBuzz.
Hint 3
A number is divisible by both 3 and 5 exactly when it is divisible by 15, so a single i % 15 check captures the overlap.
Approaches
Direct divisibility check
Time O(n) · Space O(1) extra, ignoring the O(n) output that must be returnedIterate once through 1..n and pick the output for each value using modulo tests, checking the combined 3-and-5 case before the single cases.
- Create an empty result list.
- For each i from 1 to n, test i % 15 == 0 first and push "FizzBuzz" if true.
- Otherwise test i % 3 == 0 for "Fizz", then i % 5 == 0 for "Buzz".
- If none match, push the number converted to a string.
- Return the result list after the loop.
String concatenation without explicit 15 check
Time O(n) · Space O(1) extra, ignoring the required O(n) outputBuild each entry by appending "Fizz" when divisible by 3 and "Buzz" when divisible by 5, which produces "FizzBuzz" automatically for multiples of both and avoids a separate combined test.
- For each i from 1 to n, start with an empty string s.
- If i % 3 == 0, append "Fizz" to s.
- If i % 5 == 0, append "Buzz" to s.
- If s is still empty, set it to the string form of i.
- Push s and return the list at the end.
Common mistakes
- Checking i % 3 before the combined case, so multiples of 15 wrongly become "Fizz" instead of "FizzBuzz".
- Returning numbers instead of strings; the plain values must be converted, so 4 becomes "4".
- Off-by-one ranges: the list is 1-indexed and inclusive of n, so the loop must start at 1 and run through n, giving exactly n entries.
- Using i % 3 == 0 || i % 5 == 0 as the FizzBuzz condition, which is too broad; the combined case needs both, that is i % 15 == 0.
Follow-up variations
- Make the word rules configurable, for example a map of {3: "Fizz", 5: "Buzz", 7: "Bazz"} applied in ascending key order.
- Stream or print the values for very large n without holding the whole list in memory.
- Return the count of each category (how many Fizz, Buzz, FizzBuzz, and plain numbers) instead of the full list.