Fizz Buzz in JavaScript with O(n) Time Complexity


Given a positive integer n print each number from 1 to n, each on a new line.
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.

Example:

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

Note:

Your algorithm should run in O(n) time and use O(1) space.


Understanding the Problem

The core challenge of the Fizz Buzz problem is to correctly identify and print the appropriate string or number based on the divisibility rules. This problem is significant in many coding interviews as it tests basic programming skills, including loops, conditionals, and modulo operations.

Common applications include educational tools for teaching programming and logic, as well as simple game mechanics.

Potential pitfalls include not correctly handling the order of conditions, which can lead to incorrect outputs.

Approach

To solve this problem, we need to iterate through numbers from 1 to n and apply the following rules:

A naive solution would involve checking each condition separately, but this can be optimized by checking the most specific condition (divisibility by both 3 and 5) first.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Initialize a loop that runs from 1 to n.
  2. For each number, check if it is divisible by both 3 and 5 (i.e., i % 15 == 0).
  3. If true, print "FizzBuzz".
  4. Otherwise, check if it is divisible by 3 (i.e., i % 3 == 0).
  5. If true, print "Fizz".
  6. Otherwise, check if it is divisible by 5 (i.e., i % 5 == 0).
  7. If true, print "Buzz".
  8. If none of the above conditions are met, print the number itself.

Code Implementation

for (let i = 1; i <= n; i++) {
  // Check if the number is divisible by both 3 and 5
  if (i % 15 === 0) {
    console.log("FizzBuzz");
  } 
  // Check if the number is divisible by 3
  else if (i % 3 === 0) {
    console.log("Fizz");
  } 
  // Check if the number is divisible by 5
  else if (i % 5 === 0) {
    console.log("Buzz");
  } 
  // If none of the above, print the number itself
  else {
    console.log(i);
  }
}

Complexity Analysis

The time complexity of this algorithm is O(n) because we iterate through the numbers from 1 to n exactly once. The space complexity is O(1) as we only use a constant amount of extra space for the loop variable.

Edge Cases

Potential edge cases include:

To test these edge cases, we can run the algorithm with different values of n and verify the outputs.

Testing

To comprehensively test the solution, we should include a variety of test cases:

We can use JavaScript's console.log to print the outputs and manually verify them.

Thinking and Problem-Solving Tips

When approaching such problems, it is essential to:

To improve problem-solving skills, practice solving similar problems and study different algorithms and their applications.

Conclusion

In this blog post, we discussed the Fizz Buzz problem, its significance, and how to solve it using a simple yet efficient algorithm in JavaScript. We also covered complexity analysis, edge cases, and testing strategies. Understanding and solving such problems is crucial for developing strong programming and problem-solving skills.

Additional Resources

For further reading and practice, consider the following resources: