Skip to main content
All coding problems

Fizz Buzz

EasyalgorithmsSimulation with modular arithmetic

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

Input: n = 5
Output: ["1", "2", "Fizz", "4", "Buzz"]

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.

Input: n = 15
Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]

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.

Input: n = 1
Output: ["1"]

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.

Loading the editor...

Approaches

Direct divisibility check

Time O(n) · Space O(1) extra, ignoring the O(n) output that must be returned

Iterate 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.

  1. Create an empty result list.
  2. For each i from 1 to n, test i % 15 == 0 first and push "FizzBuzz" if true.
  3. Otherwise test i % 3 == 0 for "Fizz", then i % 5 == 0 for "Buzz".
  4. If none match, push the number converted to a string.
  5. 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) output

Build 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.

  1. For each i from 1 to n, start with an empty string s.
  2. If i % 3 == 0, append "Fizz" to s.
  3. If i % 5 == 0, append "Buzz" to s.
  4. If s is still empty, set it to the string form of i.
  5. 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.

Build the rest of your prep