FizzBuzz: One Simple Interview Question
Aug 15, 2023
FizzBuzz: One Simple Interview Question
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: https://imranontech.com/2007/01/24/us … Other approaches for pretty much every language: http://rosettacode.org/wiki/FizzBuzz Thanks to the Cambridge Centre for Computing History: http://www.computinghistory.org.uk/ The thing behind me is their Megaprocessor: • The MegaProcessor And thanks to my proofreading team! I’m at http://tomscott.com on Twitter at http://twitter.com/tomscott on Facebook at http://facebook.com/tomscott and on Snapchat and Instagram as tomscottgo
Content
0.14 -> Ten years ago, a friend of mine called Imran
wrote about hiring programmers.
4.21 -> He said that he was seeing people being interviewed
6.009 -> with good computer science degrees
8 -> who, while they knew all the theory,
10.21 -> had no idea about how to actually code in
the real world.
14.09 -> And he proposed a simple test.
16 -> Now, hiring programmers is a tricky job.
18.5 -> I don't think that asking complicated
lateral thinking questions
21.939 -> is a good way to hire people,
23.339 -> and neither is picking folks who you think
will "just fit in".
26.77 -> But a simple test to make sure that someone
can actually code:
30.21 -> yeah, that makes sense.
32.489 -> So here is the test that Imran proposed:
34.34 -> code something that'll play the children's
game of FizzBuzz.
41.22 -> FizzBuzz is really simple.
42.36 -> Traditionally, it is played between two people,
44.219 -> and you count up in turn, 1, 2, 3, 4,
47.19 -> except every time there's a multiple of 3,
48.71 -> you say "Fizz" instead of the number.
50.109 -> And every time there's a multiple of 5, you
say "Buzz".
52.48 -> So: 1, 2, fizz, 4, buzz, fizz,
57.74 -> 7, 8, fizz, buzz, and so on and so on!
61.95 -> And when it's a multiple of both,
at 15, you say "FizzBuzz".
65.89 -> Right, so anyone who's going in for a job
in computer programming
70.01 -> should be able to pass this test:
71.9 -> write a program that outputs FizzBuzz for
the numbers 1 to 100.
76.299 -> And it's a clever test for a number of reasons.
78.329 -> First, there are many possible approaches.
79.68 -> I'll run through a couple of them here,
81.59 -> but you folks will be able to come up with
loads more.
83.659 -> If you want, pause the video now
and have a go at it.
86.56 -> The approach that people choose,
87.68 -> and how they work through the problems,
89.61 -> will also give you a good insight into the
style of programmer they are.
93.25 -> Do they quickly just bodge things together
95.45 -> to make it work and fix immediate problems,
98.56 -> like I do,
99.56 -> or do they start out planning for the future
and try to figure out what might change later?
103.469 -> As well as that, FizzBuzz includes something
that's a little tricky
106.939 -> to express well while programming:
109.31 -> If it's a multiple of 3, you do something.
111.36 -> If it's a multiple of 5,
you do something else.
113.729 -> If it's a multiple of both,
you do a third thing.
116.049 -> And if it's neither,
you do a fourth thing.
118.83 -> That’s not instantly translatable into code.
121.75 -> So I'm going to show you how I would do it,
124.67 -> the quick bodge-it-together approach,
126.24 -> and then we'll see how we can start
to push it a little bit further.
129.709 -> I'm writing in JavaScript: it's not the best
language in the world,
132.41 -> but it is one of the easiest
to get started with.
135.37 -> First and most obvious step: create a loop.
137.65 -> We're going to want to run through the numbers
1 to 100,
141.25 -> so we create a for loop.
143.24 -> A for loop works like this:
first, run that bit of code.
146.56 -> It creates a variable, i,
148.08 -> and it starts it out with the number 1.
150.29 -> Then every time it reaches the bottom
of the loop, where the bracket is,
152.58 -> it goes back to the top
and runs that bit of code,
155.489 -> increasing i by 1,
156.69 -> and then checks it against that bit of code.
159.2 -> If i is less than or equal to 100, it runs
this code again.
163.19 -> If not, it exits the loop.
165.33 -> So whatever's inside here?
166.5 -> We are doing it 100 times,
167.79 -> and in each loop i
is going to be increased by 1.
171.709 -> Programmers use the letter i for variables
like this because...
174.31 -> er, actually, I have no idea, it's just tradition.
177.1 -> Okay.
178.1 -> Next up.
179.1 -> We need something to output the results.
180.87 -> In JavaScript, the sort
that's in a plain text window,
183.34 -> you use console.log() for this.
184.81 -> If you were putting it in a web page you'd
use something else.
188.29 -> So: console.log(i);
190.39 -> Output whatever is in the i variable.
192.379 -> Test it, and sure enough,
you get 1 through 100.
195.2 -> Sometimes, though, we want it to output
'Fizz' or 'Buzz' instead.
198.7 -> So let's put that in an if statement.
200.38 -> If…
202.18 -> If what?
203.53 -> If i is a multiple of 3, we need to write
'Fizz'.
206.56 -> So: code.
207.6 -> That % sign is the modulo operator.
210.23 -> It returns the remainder
after you divide something.
213.34 -> So here, it's saying, divide i by 3
and return the remainder.
217.17 -> If the remainder is zero, then we know
i is a multiple of 3,
219.84 -> and we run that code.
220.909 -> And if the remainder's anything else,
222.76 -> then i is not a multiple of 3,
and we skip it.
226.04 -> So now again for 5.
228.58 -> Now what about FizzBuzz, if it's a multiple
of both 3 and 5?
231.47 -> Well, that's already been done for us, right?
233.29 -> If it's a multiple of both,
234.44 -> then both these tests are
going to return true,
236.34 -> and we get 'Fizz' and 'Buzz'.
239.019 -> So the last bit: if neither of thode are true,
241.14 -> just output the number.
242.7 -> In this case, we want that:
244.66 -> Okay, it doesn't look simple,
but I promise it is.
248.18 -> That means "not equal to".
250.02 -> And that means "and".
252.379 -> So this whole section will only return true
254.42 -> if both the bits in brackets are true.
257.54 -> Cool.
258.54 -> Let's run it.
259.54 -> And that's... well, it's OK, I guess.
260.54 -> It does the job.
261.54 -> Except... what's going on at 15?
263.05 -> That doesn't say FizzBuzz.
264.05 -> That says "Fizz" and then "Buzz".
266.73 -> Two separate lines.
267.73 -> Two separate answers.
268.73 -> That is not technically correct,
269.73 -> 'cos it needs to say 'FizzBuzz' there.
271.63 -> One line.
272.78 -> So we can add a few more tests.
274.5 -> Let's add a separate one for FizzBuzz there,
276.63 -> using what we've learned already.
278 -> If both of those tests return true, print
"FizzBuzz".
280.42 -> And then let's add some more tests in there,
so they don't fire twice.
285.14 -> This is getting quite ugly.
287.28 -> Repeating yourself like this is a sign of
dodgy code.
289.45 -> It's clumsy, it's difficult to read
and difficult to maintain.
292.72 -> So let’s change it around a bit.
294.73 -> Let’s test for both being true first.
297.67 -> Now we can use an ‘else’ statement:
299.03 -> if it’s not a multiple of both,
300.97 -> is it just a multiple of 3?
302.95 -> And if it’s not, if is just a multiple of
5?
304.53 -> And if it’s still not,
305.82 -> then we know to just output the number.
308.6 -> But to me, this is still dodgy.
310.78 -> Because if someone says,
312.03 -> 'okay, now we want it to work on
multiples of 7, not 5',
315.3 -> and that's a common sort of follow-up question
for something like this,
318.44 -> you have to remember to change all the 5s.
321.52 -> Not a big problem in a
section of code this small,
323.54 -> but once you start working on something big,
326.16 -> this is really bad practice.
327.75 -> Ideally, you shouldn't run the same test twice.
331.63 -> But how else can it be done?
333.52 -> Well, here's how I wrote this
the first time I tried it.
336.36 -> Create a variable called "output" that's just
an empty string.
339.34 -> A blank bit of text, nothing in it.
341.94 -> If i is a multiple of 3, you add "Fizz" to
that line of text.
345.32 -> You don't output anything yet,
you just store that for the future.
348.19 -> If i is a multiple of 5, you add "Buzz" to
that line of text.
351.24 -> Don't replace it: just add it.
353.45 -> And then, don't test the number again --
test that string of text.
356.25 -> If it's empty,
357.64 -> we know none of these tests were true,
359.44 -> so you just make the text the value of i.
362.08 -> Now at the end of the loop, you output…
well, the output.
365.72 -> So now, if whoever's running the interview
says
367.8 -> 'make it work on 7s, not 5s',
369.91 -> you only have to change one thing
in an obvious place.
372.92 -> If they say 'make it work on
3s, 5s, 7s, 11s and 13s',
376.55 -> you just copy and paste that line
and it just does.
379.54 -> Programming that with else statements
would be a nightmare.
383.02 -> Now I think there are still
better ways to code this,
385.34 -> particularly if you wanted to plan
long into the future.
387.62 -> After all, those lines there...
389.88 -> I'm technically repeating myself.
392.58 -> But I'll leave fixing that as a problem for
someone else.
395.3 -> This is good enough for me.
396.49 -> Now there are still some companies who use
this particular test for hiring,
400.83 -> particularly because, y'know, some interviewers
can’t think of anything better.
405.31 -> But there are also many problems like it:
407.06 -> things that a lot of computer science degrees
don't teach
408.94 -> because they're all about the theory.
411.53 -> Me?
412.53 -> I'm more in favour of bodging things together
and making them work well enough.
415.98 -> Just don't leave too much of a mess
418.02 -> for whoever comes along to maintain your code
once you're done with it.
422.48 -> Thank you to everyone here at the Centre for
Computing History in Cambridge
425.48 -> for letting me film
426.8 -> and thank you to all the proofreading team
who helped make sure I got my script right.
Source: https://www.youtube.com/watch?v=QPZ0pIK_wsc