Mark As Completed Discussion

Good morning! Here's our prompt for today.

Fizz Buzz is a classic interview question that apparently many experienced engineering candidates often can't solve! Let's cover it today.

We're given a number in the form of an integer n.

Write a function that returns the string representation of all numbers from 1 to n based on the following rules:

  • If it's a multiple of 3, represent it as "fizz".

  • If it's a multiple of 5, represent it as "buzz".

  • If it's a multiple of both 3 and 5, represent it as "fizzbuzz".

  • If it's neither, just return the number itself.

As such, calling fizzBuzz(15) would result in '12fizz4buzzfizz78fizzbuzz11fizz1314fizzbuzz'.

The following image provides a visual of how we processed the input:

Description

Constraints

  • Maximum value of Integer n <= 1000
  • n will always be a positive integer, but can be 0
  • Expected time complexity : O(n)
  • Expected space complexity : O(1)

AlgoDaily partner CodeTips.co.uk has kindly provided a guide on solving Fizz Buzz in Go and Javascript. Check it out for even deeper coverage of this problem.

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's a video of us explaining the solution.

To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's our guided, illustrated walk-through.

How do I use this guide?