One Pager Cheat Sheet

  • We are given an integer n, and asked to write a function fizzBuzz() that returns the string representation of all numbers from 1 to n using the rules "fizz", "buzz" and "fizzbuzz" for multiples of 3, 5 and both respectively, with a maximum value of 1000, O(n) time complexity and O(1) space complexity.
  • The key to answering this technical interview question was to identify how to map a problem to its technical implementation, such as using if-else or switch control flows.
  • We can use the modulo operator (%) to easily determine if one number is a multiple of another - the remainder returned will be 0 if it is.
  • We can use a conditional flow order of fizzbuzz > fizz > buzz > return number to check if a number is divisible by either 3, 5, both, or neither, with a time complexity of O(n).

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT

Got more time? Let's keep going.

If you had any problems with this tutorial, check out the main forum thread here.