One Pager Cheat Sheet
- We are given an integer
n, and asked to write a functionfizzBuzz()that returns the string representation of all numbers from1tonusing the rules "fizz", "buzz" and "fizzbuzz" for multiples of 3, 5 and both respectively, with a maximum value of1000,O(n)time complexity andO(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-elseorswitchcontrol flows. - We can use the
modulo operator(%) to easily determine if one number is a multiple of another - the remainder returned will be0if it is. - We can use a conditional flow order of
fizzbuzz>fizz>buzz>return numberto check if a number is divisible by either3,5, both, or neither, with a time complexity ofO(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.
xxxxxxxxxx58
}var assert = require('assert');​function fizzBuzz(n) { let finalArr = [];​ for (let i = 1; i <= n; i++) { if (i % 3 === 0 && i % 5 === 0) { finalArr.push('fizzbuzz'); } else if (i % 3 === 0) { finalArr.push('fizz'); } else if (i % 5 === 0) { finalArr.push('buzz'); } else { finalArr.push(i.toString()); } }​ return finalArr.join('');}​try { assert.equal(fizzBuzz(0), '');​ console.log('PASSED: ' + "Expect `fizzBuzz(0)` to equal `''`");} catch (err) { console.log(err);}​try { assert.equal(fizzBuzz(1), '1');​ console.log('PASSED: ' + "Expect `fizzBuzz(1)` to equal `'1'`");OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Great job getting through this. Let's move on.
If you had any problems with this tutorial, check out the main forum thread here.

