FizzBuzz: One Simple Interview Question

  • Published on: 31 July 2017
  • There are a lot of opinions on how to hire coders, and most of them are terrible. The opinions, that is, not the coders. But a basic filter test to make sure someone can do what they say they can: that seems reasonable, and FizzBuzz is one of the more common tests. Even now, interviewers use it. Let's talk about why it's tricky, and how to solve it.

    Imran's blog post:

    Other approaches for pretty much every language:

    Thanks to the Cambridge Centre for Computing History:

    The thing behind me is their Megaprocessor:

    And thanks to my proofreading team!

    I'm at
    on Twitter at
    on Facebook at
    and on Snapchat and Instagram as tomscottgo
  • Runtime : 7:18
  • tom scott tomscott the basics computer science fizzbuzz interview interview question fizzbuzz test comp sci javascript


  • Shane Wright
    Shane Wright   1 days ago

    I used to ask designer/developers this question:"You are wrongfully accused of murder. You ave 72 hours in which to prove yourself innocent and name the real killer, but you can't leave your prison cell. You are, however, allowed to hire 1 TV detective to investigate on your behalf. Which Detective do you hire?"80% of the applicants, regardless of age, speciality, ethnic background or education level, said "Columbo". Which wasn't helpful, because that is the right answer.

  • Denis Franco
    Denis Franco   1 days ago

    a bit more functional approach:const dictionary = [ { key: 3, value: 'Fizz' }, { key: 5, value: 'Buzz' }]let iconst isDivider = entry => i % entry.key === 0for (i = 1; i <= 100; i++) { if (!dictionary.some(isDivider)) { console.log(i) } else { let output = '' dictionary .filter(isDivider) .forEach((entry) => { output += entry.value }) console.log(output) }}

  • zzpza
    zzpza   3 days ago

    All seemed perfectly logical to me, but then I've been programming for longer than I care to remember 😉

  • Jaz Akbar
    Jaz Akbar   3 days ago

    I am not a programmer but I can handle VBA. This is exactly how I coded it when said to pause and try.

  • Financeification
    Financeification   4 days ago

    Base Rfizzbuzz<- function(x){ paste0( ifelse(x%%3 == 0,'fizz',''), ifelse(x%%5 == 0,'buzz','') ) }fizzbuzz(1:100)

  • Flynn The Soulless Demon

    My attempt in JS:const tests = ["3:Fizz","5:Buzz"];if (tests.length != 0) { for (var i = 0; i < 100; i++) { var output = ""; for (var j = 0; j < tests.length; j++) { if (i % tests[j].split(":")[0] == 0) output += tests[j].split(":")[1]; }; if (output == "") {output = i;} console.log(output); };}else { console.log("Error: No Tests Given")};

  • Arthur Paliden
    Arthur Paliden   5 days ago

    'i' is as hold over from the FORTRAN days. IAVO .....In FORTRAN variables with names starting I, J, K, L, M and N are integer variables

  • Steve's teacher
    Steve's teacher   6 days ago

    I liked to modify the code to be an ES6 if statement... Although I would not recommend using this method in an interview tho :)var fizz = 3;var buzz = 5;for (let i = 1; i <= 100; i++){ (i % fizz == 0) && (i % buzz == 0) ? console.log("FizzBuzz") : i % fizz == 0 ? console.log("Fizz") : i % buzz == 0 ? console.log("Buzz") : console.log(i);}JavaScript is my favourite programming language

  • Frank Upton
    Frank Upton   6 days ago

    Rather than 'if it divides by 3 exactly and divides by 5 exactly' ask 'if it divides by 15 exactly'.

  • Raul Cavalcante
    Raul Cavalcante   1 weeks ago

    i did it in python:steps = 100 numbers = [[3, "Fizz"], [5, "Buzz"]] for i in range (1, steps): output = "" for a in numbers: if i % a[0] == 0: output += a[1] if output == "": output = i print(output)

  • Derrick Melton
    Derrick Melton   1 weeks ago

    Use two aligned arrays to define the spec and iterate over its elements. If the outer iterator value mod array element at tue inner iterator is 0, append the value stored at the same index in the other array to the string.Mostly generic fizzbuzz alg.

  • Steve
    Steve   1 weeks ago

    Saw this awhile ago, but had to do it in bash for an interview today.

  • Nnay
    Nnay   1 weeks ago

    practically everyone I know would just do a loop and check for the modulo.

  • Andrew Tieszen
    Andrew Tieszen   1 weeks ago

    2:50I have no idea if this is the actual reason, but I've always considered i to be a shorthand for "increment." If this is true, the convention probably originated with assembly code.

  • Evan Yoohoo
    Evan Yoohoo   1 weeks ago

    The string builder approach is clever, which half the time is a red flag.It's also not semantic, in particular you are writing fizzbuzz in two separate actions and storing information in the emptiness of the string.The extensibility is also superficial, there's no reason to assume that the next change will be adding an additional word that follows similar rules.Finally, the performance is suboptimal due to concatenation instead of returning references to static strings.

  • Krystine Mason
    Krystine Mason   1 weeks ago

    Interesting question :D, I knocked up a quick test using Forth-79 in under a minute, including testing :D. Fairly simple problem using a modular operand.

  • WarpRulez
    WarpRulez   1 weeks ago

    Obviously the correct answer is to implement a completely generic solution that can be given any amount of numbers with an associated word, and it will output those words and the proper combinations of those words when the numbers in the sequence are divisible by them. (And, of course, you should be able to tell it where to output it to.)

  • Hand Solo
    Hand Solo   1 weeks ago

    I worded at a place where they made you code a function that would convert roman numerals from 1 to 20. Problem with fizzbuzz is it doesn't tell you if they can code, only if they cant.

  • Wolfram
    Wolfram   1 weeks ago

    So people who can't do fizzbuzz get interviews, but I don't get anything for my mechanical major. And yet we all regularly can do something like fizzbuzz in mechanical.

  • David Frogley
    David Frogley   1 weeks ago

    "i" is for "index" or "iterator", I think, given that for-loops iterate over index-based arrays.

  • Commentur The Great
    Commentur The Great   1 weeks ago

    //This is how you would do it if you had to make it in the best possible way (I am unfamiliar with javascript so there may be some mistakes):for (var i = 1; i <= 100; i++) { var specials = [Special(3, "Fizz"), Special(5, "Buzz")]; console.log(getOutput(i, specials));}function getOutput(i, specials) { var output = getSpecialOutput(i, specials); if (output == "") { output = i; } return output;}function getSpecialOutput(i, specials) { var output = ""; for (var i = 0; i < specials.length; i++) { special = specials[i]; output += fizzBuzz(i, special.num, special.string); } return output;}function fizzBuzz(i, num, string) { if (i % num == 0) { return string; } return "";}class Special { constructor(num, string) { this.num = num; this.string = string; }}//I am starting to think it may have been simpler to do it his way...

  • whyme943
    whyme943   1 weeks ago

    So, if CS degrees teach theory, whats the best place to learn practical coding skills?

  • Shade's channel
    Shade's channel   1 weeks ago

    I think originally "i" was used for "index" and "increment". People subsequently used j and k because they followed i, even if they weren't related.

  • Zachary Reed
    Zachary Reed   1 weeks ago

    I took a python class in high school (graduated '18), and hadn't really been coding since. I don't know why, I use to love coding and playing with my dad's Arduinos and Pi's. This video convinced me to make a little project for myself (mood lighting for my turntable that turns off if my room is too bright). I ordered all the parts I think I'll need. Thank you for reminding me of something I use to love, Tom.

  • Davy Ker
    Davy Ker   1 weeks ago

    i has been used as a variable for sums and other loop-type things in mathematics for centuries. I imagine that's where programmers get it from. It's to do with how we are saying that i is a member of some indexing set.

  • Sowel 44
    Sowel 44   1 weeks ago

    This would really be an interview problem?

  • Silvonch
    Silvonch   1 weeks ago

    I was always told the "I" meant "index"

  • Cryptomang
    Cryptomang   1 weeks ago

    I stands for Index and is the reason people use that character in for loops

  • Avoyt
    Avoyt   2 weeks ago

    Aaa! I decided to try my hand at it in the Pico-8 environment (Lua)! I added an extra bit where it appends an exclamation mark if all the parameters are met. For example, by default, 15 returns "FIZZBUZZ!".For the uninitiated, Pico-8 Lua is in all-caps, so don'T FREAK OUT! Lemme know if you see how I can improve though!-- FIZZBUZZ 20190904// AS AN ORDERED OBJECT TO KEEP// THE PARAMETERS IN ORDER_PARAM = { {'FIZZ',3}, {'BUZZ',5},}FOR I=1,100 DO LOCAL STR LOCAL J=0 FOR P IN ALL(_PARAM) DO IF I%P[2]==0 THEN STR = (STR OR '')..P[1] J+=1 END END IF (J==#_PARAM) STR = STR..'!' PRINT(STR OR I)END

  • Avoyt
    Avoyt   2 weeks ago

    𝚒𝚖𝚙𝚘𝚛𝚝 𝚏𝚒𝚣𝚣𝚋𝚞𝚣𝚣

  • GuvernorDave
    GuvernorDave   2 weeks ago

    Videos like this give me the false hope that I'll ever be able to wrap my head around coding.

  • Rotem JACKMAN
    Rotem JACKMAN   2 weeks ago

    Not the shortest version but maybe more readable for the future.for(i=0; i<=100; i++){ fizz= (i%3==0) ? 'fizz':'';buzz= (i%5==0) ? 'buzz':'';console.log(!fizz && !buzz ? i : fizz+buzz); }