FizzBuzz

EASY

Write a program that generates a list of strings representing numbers from 1 to n (inclusive). However, for multiples of two, output "Fizz" instead of the number. For multiples of three, output "Buzz". For numbers which are multiples of both two and three, output "FizzBuzz".

(The problem often asks to print, but returning a list of strings is common in coding platforms).

Examples (for n = 10):

Input: n = 10
Output: ["1", "Fizz", "Buzz", "Fizz", "5", "FizzBuzz", "7", "Fizz", "Buzz", "Fizz"]

Expected output for specific numbers:

  • 1 -> "1"
  • 2 -> "Fizz" (multiple of 2)
  • 3 -> "Buzz" (multiple of 3)
  • 4 -> "Fizz" (multiple of 2)
  • 5 -> "5"
  • 6 -> "FizzBuzz" (multiple of 2 and 3)

Constraints:

  • 1 <= n <= 104

Function Signature (Python):

from typing import List

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        # Your code here
        pass

 

Nerchuko Academy · Free DS Interview Prep