oneby7

Foundations Loops

588. Fizz Buzz

Easy

Given an integer n, return a list of n strings, one for each whole number from 1 to n, in that order. The string for a number is:

  • "FizzBuzz" if the number is a multiple of both 3 and 5,
  • "Fizz" if it is a multiple of 3 but not of 5,
  • "Buzz" if it is a multiple of 5 but not of 3,
  • the number itself, written in digits, if it is a multiple of neither. So 7 gives "7".

A multiple of 3 is a number that 3 divides with no remainder, such as 3, 6 or 9.

Example 1

Input: n = 15

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

Explanation: 3, 6, 9 and 12 are multiples of 3 only, so they give "Fizz". 5 and 10 give "Buzz". 15 is a multiple of both 3 and 5, so it gives "FizzBuzz".

Example 2

Input: n = 1

Output: ["1"]

Explanation: 1 is not a multiple of 3 or of 5, so the list holds just "1".

Example 3

Input: n = 5

Output: ["1","2","Fizz","4","Buzz"]

Explanation: The list ends with n itself. 5 is a multiple of 5, so the last string is "Buzz".

Check your understanding

Work out the answer for this input before you write any code.

n = 3

Constraints

  • 1 ≤ n ≤ 105

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.