Bewitching the Coding Interview

A spoof. With thanks (and apologies) to Aphyr.


The interviewer greets you as you step into the bright, open workspace. Sunlight streams onto his rugged, handsome face as he introduces himself. “Kris”, he says. A fine name.

“Hello! I’ve, um, heard some things,” he stammers. You smile.

“Have you really?”

“Yes, well, ah — I know you’re quite fond of your functional programming. But just remember this is a Python position, and we’ll be looking for best practices. Maintainable code, that sort of thing.”

Your smile broadens. “Best practices? Of course! What shall we start with?”

He seems relaxed now, on stable ground. “Great! Let’s write a function to calculate the nth fibonnacci number.”

You breathe deeply, and feel once again the hot glow of power expand through your chest and push its way to your fingertips.

“Did you bring your own computer?”, he queries. You have no idea what he is talking about. Computers were forgotten long ago, in the dawning of the Third Age, when such sacred spells were set free of the bonds of metal and electricity. Still, you allow him to guide you to a matte-white Macintosh Computer, and open your IDE of old: a Terminal window.

user@mac~$ python3 -m venv venv && source ./venv/bin/activate

“Oh, you don’t need to worry about—”

You silence him with a sharp look. “Best practices, right?”

“Sure, but we’ll just foc—”

But his protestations are in vain as, burning with a spell yearning to be released, you have already started the initial Rites of Power:

user@mac~$ export PS1="ᛈᛃᚦᛟᚾ> " && python3 -m pip install flask && pip freeze >> requirements.txt

“Wait, Flask? Like the web thingy? And are those... runes?”

You pay no mind. The intricacies of eldritch magick may not be revealed to those yet to be named by the Aesir.

ᛈᛃᚦᛟᚾ> cat << ᚦᛖ᛫ᛒᛁᛏᛏᛖᚱ᛫ᛖᚾᛞ | python
ᛈᛃᚦᛟᚾ> from functools import wraps
ᛈᛃᚦᛟᚾ> from types import GeneratorType

Kris wipes off a bead of sweat from his brow, and opens his mouth as if to question your methods. But you’re already typing furiously, and—as if to comfort him—you allow for a somewhat familiar pattern to pierce the veil of higher understanding.

ᛈᛃᚦᛟᚾ> def fibonnacci(n, a=1, b=1):
ᛈᛃᚦᛟᚾ>     if n <= 1:
ᛈᛃᚦᛟᚾ>         yield b
ᛈᛃᚦᛟᚾ>     yield fibonnacci(n - 1, b, a + b)

“Ah, good. That looks like just the sort of solution we were looking for! And it looks like it’s tail-recursive, is that right?”

He’s physically relaxed now at the sight of a familiar function, but his eyes flicker as he notices the ‘yield’ in place of ‘return’. Kris doesn’t want to look at the yield statement. It scares him more than he cares to admit. An involuntary shudder passes through him as he remembers whispers of your past: the sacred birth of fire and stone; the template metaprogramming; the mournful easterly winds on a forgotten shore with no name.

“So what’s that ‘yield’ doing,” he continues, steadying himself, “isn’t that for generators or something?”

Ah, Kris. Young, but spirited. He could make a fine and noble warrior one day, but the path of witchcraft eludes many like him.

“Generators are for lazily evaluating things, Kris,” you explain brightly. “And lazy evaluation is always good!”

He opens his mouth to reply. “ᛖᛪᛈᛚᛁᚲᛁᛏ᛫ᛒᛠᛏᛋ᛫ᛁᛗᛈᛚᛁᚲᛁᛏ”, you remind him, with a tinge of warmth behind your words. Seizing the opportunity, you let the heat of power spread within you and lay the foundations to setting your new-forged spell free.

ᛈᛃᚦᛟᚾ> from flask import Flask, request
ᛈᛃᚦᛟᚾ> 
ᛈᛃᚦᛟᚾ> app = Flask(__name__)
ᛈᛃᚦᛟᚾ> 
ᛈᛃᚦᛟᚾ> 
ᛈᛃᚦᛟᚾ> @app.route("/fib")
ᛈᛃᚦᛟᚾ> def fib_route():
ᛈᛃᚦᛟᚾ>     try:
ᛈᛃᚦᛟᚾ>         val = int(request.args["number"])
ᛈᛃᚦᛟᚾ>         return str(cached_unroll(fibonnacci)(val))
ᛈᛃᚦᛟᚾ>     except ValueError as e:
ᛈᛃᚦᛟᚾ>         return "Bad request: " + str(e), 400

Kris’ eyes have slightly lost their focus, but to his credit, he manages to ask, "What... what are we using Flask for again?”

“Everything is a resource to us, Kris. Much like water, food, and shelter, we rely on mathematics to keep us alive. To represent it as anything other than a resource, open to the world, is to deny our essential humanity.”

“Ah... right. And the ‘cached_unroll’ call, what does that do?”

You’re glad he asked. You let your eyes roll into the back of your skull, and feel your body briefly transcend the mortal plane. Astral signs and ancient runes burn in your retinas as you cast a powerful dual incantation, upon which you later think Baldr himself might have looked proudly.

ᛈᛃᚦᛟᚾ> def cached_unroll(fn):
ᛈᛃᚦᛟᚾ>     seen = {}
ᛈᛃᚦᛟᚾ> 
ᛈᛃᚦᛟᚾ>     @wraps(fn)
ᛈᛃᚦᛟᚾ>     def wrapped(*args, **kwargs):
ᛈᛃᚦᛟᚾ>         params = (args, frozenset(kwargs.items()))
ᛈᛃᚦᛟᚾ>         if params in seen:
ᛈᛃᚦᛟᚾ>             return seen[params]
ᛈᛃᚦᛟᚾ>         result = fn(*args, **kwargs)
ᛈᛃᚦᛟᚾ>         while isinstance(result, GeneratorType):
ᛈᛃᚦᛟᚾ>             result = next(result)
ᛈᛃᚦᛟᚾ>         seen[params] = result
ᛈᛃᚦᛟᚾ>         return result
ᛈᛃᚦᛟᚾ> 
ᛈᛃᚦᛟᚾ>     return wrapped

Some time afterwards, you wake, refreshed and comforted by the warmth of your magick’s close embrace.

“We’re all done, Kris!”, you say, brightly.

You coax him, shuddering, back to his feet. You know now that he cannot join your kind, and could serve merely as a foot-soldier in the legions of the Vanir.

Two brief mutters of arcana are enough to complete the task set in front of you by this mortal, but they serve only as an exuberance. In your mind, you already wander the great barren planes of lands afar.

ᛈᛃᚦᛟᚾ> app.run()
ᛈᛃᚦᛟᚾ> ᚦᛖ᛫ᛒᛁᛏᛏᛖᚱ᛫ᛖᚾᛞ
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

The task is done, and you melt into the mists and the darkness. Kris, as if waking from some deep reverie, rubs his eyes. Looking around, and finding nothing but his Macintosh, he opens a new Terminal window.

user@mac~$ http localhost:5000/fib number==500
HTTP/1.0 200 OK
Content-Length: 105
Content-Type: text/html; charset=utf-8
Date: Thu, 28 Dec 2017 01:29:07 GMT
Server: Werkzeug/0.13 Python/3.6.0a2+

225591516161936330872512695036072072046011324913758190588638866418474627738686883405015987052796968498626

Somewhere to the east, a hair’s breadth above the horizon, a glimmer of light shines briefly, and is gone.