The Algorithm

Explore the FizzBuzz solution with detailed code explanation

Algorithm Approach

Iterate from 1 to N. For each integer, check divisibility by the Fizz divisor, the Buzz divisor, and both. Assign the appropriate label — Fizz, Buzz, FizzBuzz, or the number itself — and collect the results.

Complexity Analysis

Time Complexity:
O(n) Single pass through 1..N
Space Complexity:
O(n) Result array of N items

Step-by-Step Process

  1. Loop i from 1 to N (inclusive)
  2. Check i % fizz === 0 and i % buzz === 0
  3. If both are true → push "FizzBuzz"
  4. Else if only Fizz → push "Fizz"
  5. Else if only Buzz → push "Buzz"
  6. Otherwise → push the number as a string

Implementation

Example Walkthrough (Fizz = 3, Buzz = 5, N = 15)

i i % 3 i % 5 Output
1111
2222
303Fizz
4144
520Buzz
601Fizz
7127
8238
904Fizz
1010Buzz
112111
1202Fizz
131313
142414
1500FizzBuzz

Try It Yourself

Use the interactive solver to generate sequences with any divisors.

Go to Solver

Source Code

Browse the full repository on GitHub.

View on GitHub