How long should it take to solve the FizzBuzz coding question?

So, it was past midnight, and I was pretty much done for the day when one of those random thoughts ran past me like a fleeting glimpse (thanks, Roger) and I just couldn’t let go: if I were asking a candidate to solve the FizzBuzz problem, how long would be fair to expect them to solve it?

Well, the only way to find out was for me to solve and expect someone not to take longer than, say twice as much.

This is then what I set out to find out: how fast can I code the solution FizzBuzz, tired and having had a nice Margarita and a cerveza with my Mexican dinner?

No help, just a terminal, Python and me, bare-handed… let’s find out how fast you can code, sunshine!

┌─ /tmp                             ☸ 0:20
└─( vim fizzbuzz.py

(thanks, zsh for keeping the time)

┌─ /tmp                             ☸ 0:23
└─( python fizzbuzz.py 33
  File "fizzbuzz.py", line 7
    print(f"{i}: ")
                 ^
SyntaxError: invalid syntax

Dammit, of course, MacOS is a beautiful OS and I love it dearly, but, yeah, it ships with Python 2.7 (Note to self: invite anyone still using Python 2.7 to kindly leave, now).

So, virtualenv to the rescue – always good to keep a few virtual envs sloshing around, running anything from Python 3.7 onward, and having ipython, jupyter and, ideally pandas and numpy at the ready too (but, no, not using them – I promised, “bare handed fight”):

┌─ ✘  /tmp                         ☸ 0:23
└─( workon dev

Dammit, Marco, wass’up with you tonight?

┌─ (dev)  /tmp                     ☸ 0:23
└─( python fizzbuzz.py 33
Traceback (most recent call last):
  File "/private/tmp/fizzbuzz.py", line 6, in <module>
    for i in range(n):
TypeError: 'str' object cannot be interpreted as an integer

Of course, sys.argv is an array of strings and Python by itself is not smart enough to figure out a numerical argument (children: do NOT use sys.argv!! argparse is what you want):

┌─ (dev)  /tmp                     ☸ 0:24
└─( python fizzbuzz.py 33
0:
fizzbuzz
1:
fizzbuzz
2:
fizzbuzz
3:
fizz
4:
fizz
......

Ummm, yeah, it runs, but really, most definitely a bug there (note how 0 is the correct answer, as it is by definition divisible by all integers, but it shouldn’t really be there): obviously, out needs to be initialized inside that loop, you dummy!

┌─ (dev)  /tmp                     ☸ 0:24
└─( python fizzbuzz.py 33
0:
fizzbuzz
1:

2:

3:
fizz
4:

5:
buzz
6:
fizz
7:
....

Ok, not bad, 4 minutes to the correct answer, but, holy moly, does the output looks ugly (and, yeah, don’t use print to emit your output, unless it’s really something trivial you’re doing: either use the logging module, or prettify it with cprint).

┌─ (dev)  /tmp                     ☸ 0:25
└─( python fizzbuzz.py 33
1:
2:
3: fizz
4:
5: buzz
6: fizz
7:
8:
9: fizz
10: buzz
11:
12: fizz
13:
14:
15: fizzbuzz
....

This is better and I decided to invest a few seconds to “prettify” the code and add the shebang and the authorship (hence the extra minute) but still not exactly what we want; however, I would say that by this stage of the exercise, I would consider it “problem solved”, the rest is just eye-candy:

┌─ (dev)  /tmp                     ☸ 0:26
└─( chmod +x fizzbuzz.py

┌─ ✘  (dev)  /tmp                  ☸ 0:26
└─( ./fizzbuzz.py 33
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz

Ah, finally – this looks pretty, the code looks nice, and it runs how it was meant to be.

So, there you have it fellas: if it takes you longer than around 5 minutes to arrive at the solution of FizzBuzz (in whatever language takes your fancy) you’re probably not fit to be a senior developer, and longer than 10 poses serious questions about your coding ability overall.

Peace.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: