Magicsheet logo

Fizz Buzz

Easy
69.8%
Updated 6/1/2025

Fizz Buzz

1. What is this problem about?

The Fizz Buzz interview question is the classic "coding 101" task. You need to return a list of strings representing numbers from 1 to nn. However:

  • If the number is divisible by 3, output "Fizz".
  • If it's divisible by 5, output "Buzz".
  • If it's divisible by both 3 and 5, output "FizzBuzz".
  • Otherwise, output the number itself as a string.

2. Why is this asked in interviews?

This is the most famous "sanity check" in the industry, used by companies like Microsoft and Goldman Sachs. It’s designed to filter out candidates who cannot write a basic loop with conditional logic. It tests your ability to handle multiple conditions and ensures you understand the order of evaluation (checking for "FizzBuzz" first).

3. Algorithmic pattern used

This problem follows the Simulation and Conditional Logic pattern.

  1. Iterate: Loop from i=1i = 1 to nn.
  2. Order of Checks:
    • Check i % 15 == 0 first (or i % 3 == 0 AND i % 5 == 0).
    • Then check i % 3 == 0.
    • Then check i % 5 == 0.
  3. String Concatenation (Optional): Some people prefer building a string: if divisible by 3 add "Fizz", if by 5 add "Buzz". If empty, use the number.

4. Example explanation

n=5n = 5

  1. 1: Not divisible by 3 or 5. Output "1".
  2. 2: Not divisible. Output "2".
  3. 3: Divisible by 3. Output "Fizz".
  4. 4: Not divisible. Output "4".
  5. 5: Divisible by 5. Output "Buzz". Result: ["1", "2", "Fizz", "4", "Buzz"].

5. Common mistakes candidates make

  • Wrong check order: Checking i % 3 before i % 15. This would result in "Fizz" instead of "FizzBuzz" for numbers like 15.
  • String Conversion: Forgetting to convert the integer to a string when no divisibility rules apply.
  • Hardcoding 15: Not being able to explain why 15 is used (the least common multiple of 3 and 5).

6. Interview preparation tip

Even for simple problems, strive for Clean Code. If the conditions were to change (e.g., adding "Jazz" for 7), using a string-building approach is much more maintainable than writing 232^3 if-else blocks.

Similar Questions