Walkthrough of my coding interview
Recently my team has been growing.
This means that I get to interview people and dispense live coding challenges.
I started writing this walkthrough as something to send to interviewed candidates after being asked how to properly solve the challenge and I ended up going out of scope.
Anyhow I hope you enjoy this post!
The main problem with live challenges
My main issue with live challenges stems from the fact that trying to condense our entire profession into a half an hour excercise in algorithmic prowess is not only disrepectful to the profession but also tortuous to the person being interviewed.
There is really no way to determine, in an hour, if you are an able programmer or to query the depth of knowledge each person has.
Even more so when you take into account we are taking people for a back-end position where we have a lot of tasks which are not algorithmically complex but rather consist of sitting down and thinking how to structure our interfaces in such a way that won't end up being a pain to work with.
So the idea is to create a very easy challenge which leaves room for creativity and try to evaluate "how you think and communicate problems".
The actual challenge
Disclaimer: I thought this challenge was going to be very easy and no one would have problem solving it. After a few interviews it turned out this was not the case.
This mismatch between expectation and reality is what ended up prompting this Walkthrough.
I am also NOT in favor of live technical challenges.
I can see most of what you've worked on from your portfolio/cv and if I have any doubts I can just ask, I'm yet to meet a programmer who isn't able to talk hours about their personal projects.
Sadly managment asked me to give a live coding challenge as part of the recruitment process and a take home was denied for fear of chatGPT.
Anyway let us move on to the topic I said I would write about.
The exercise
The challenge consists of the following:
Given the following input:
a = 0
a = a + 1
print
We should print something like:
a : 1
The challenge is vague on purpose, I always emphasize that it isn't really important whether they manage to get to a good solution or even, sometimes, a solution at all.
I'm more interested in what does this person see as relevant or important.
Normally after a few questions we will get to the following considerations:
- Variables will always be initialized before being referenced
- We only need to support addition
- There will only be two types of statements: prints and assignation
- All numbers will be natural numbers including the 0
- A solution which deals with the naive case of just one variable will be accepted as correct
- You can leave error handling for later if it is more comfortable, just assume all sentences are valid
- You can Google/DuckDuckGo/Whatever without fear
- There will be no multi-line statements
The challenge is made to be as comfortable as possible so things like robust error checking, type hinting or testing can be left out.
Of course we will rate you favorably if you include them but we won't demand it.
Making a first approach
So! Let's get our hands typing!
To avoid getting overwhelmed we recommend writing a first approach which handles the first and last statement.
This would mean handling the following input:
a = 0
print
Once you can handle "a = 0" and "print" it's a lot clearer how to proceed.
We can also forget about how to handle input and assume we already have it since I don't really think that reading from a file or from stdin really adds anything interesting to the challenge.
So first things first we will start with a:
data = "a = 0\nprint"
So now that we have our data we should check what our sentences are and figure out what to do with them.
I know this is a bit obvious but in the stress of an interview you can panic and enter into a doom spiral.
I think that is why a lot of devs we interviewed get stuck in the first step, they get overwhelmed by the task and we need to remind them to:
¡Split it up!
for line in data.split('\n'):
print(line)
¡Voila!
This will just print the statements but we are sort of getting somewhere. We could have also hardcoded the indexes and go for a 100% hardcoded solution before looking for a general approach but I think we have enough evidence to appreciate that a general approach won't be much more complicated and may be even easier to write.
What I think is a good second step is to decide that, for now, we will only handle one hardcoded variable and the print statements.
In more clear terms: we will set have a global variable a which will also be our only variable and whenever we find a print statement we will print it.
a = 10
for line in data.split('\n'):
if line == "print":
print(f"a : {a}")
¡Easy!
I find it nice to set a to a value we do not want to remind ourselves this is not a real solution.
Even though this is not a real solution we can sort of handle assignment by just adding 2 lines:
a = 10
for line in data.split('\n'):
if line == "print":
print(f"a : {a}")
else:
a = line.split('=')[1].strip()
Here a will just be a string and for now this is okay. It works.
It might not be that nice and we are still far from getting to something satisfactory but if you have gotten this far then congratulations! You've gotten farther than 50% of our applicants.
Now let's go ahead and try to turn this into a solution.
Making it work
We could keep on cramming behaviour into this functions but I think it would be nice to tidy this up a little bit so lets define a function for handling statements.
if line == "print":
print(f"a : {a}")
else:
a = process_assignment(line)
Here process_assignment will just return the right side of the statement but... what is the right side of the statement? what about multiple variables? what about assignments where we have a sum? what about multiple sums inside of the same assignment?
This are all very valid questions that I'm sure you have but let us slow down a bit.
We are still solving the case where we have only one variable "a" so we can forget about multiple variables for now and focus on properly processing the assignments.
Assignments will always have two "sides" divided by the '=' char.
As an example the assignment "a = 25 + 1" has a left side of a and a right side 25 + 1
For now the left side, the variable name, will be ignored.
The right side, the actual expression to be calculated, will either consist of a number of variables separated by plus signs which will be added to get the final result of the expression or it will consist of a variable name or number.
So let's make a function which will process this right side:
def process_expression(e):
res = 0
for var in e.split('+'):
if var.strip() == 'a':
res += a
else:
res += int(var)
return res
Since we are still handling the case where a is the only possible variable we can say that all the elements in the expression will either be a or a number
By adding up all the elements in the expression we get the result of the expression and we return it.
Now we need to have assignments properly handle expressions and we will have a good enough solution.
So let us hook everything up!
def process_assignment(line):
expression = line.split('=')[1].strip()
return process_expression(expression)
def process_expression(e):
res = 0
for var in e.split('+'):
if var.strip() == 'a':
res += a
else:
res += int(var)
return res
By this point you have gotten farther than most of our applicants and you are probably hired.
Now is the moment where, if there is enough time left, I would ask you how would we handle multiple variables (just add a dict) and if we still have time then we will talk about multiple operators and abstractions and maybe your feelings regarding OOP.
So far this has been pure fantasy.
No interview has gotten this far.
Leaving fiction behind
I already mentioned that I thought that this interview was going to be easy, a breeze, something to start the conversation and get moving while also complying with management decission to include a technical challenge.
This has not been the case. All candidates interviewed had great difficulty solving this challenge. Even people with computer science degrees who had taken, at some point, a compilers course.
Maybe it was the ambiguity of the challenge, a lot of times I think nervousness was greatly involved, maybe I am just bad at preparing interviews.
I don't want nor need someone who can recite the entire stdlib by memory or a "10x engineer" and I am not looking for the perfect candidate but I am a bit surprised by this turn of events and I am wondering what could be a better technical challenge.
Ideally the challenge would involve parsing since a lot of our daily tasks are parsing related and if possible it would be nice to have an excuse to talk about multithreading.
It should also be easy as to not overwhelm the candidate but not so easy that managment will get angry with me.
I think there is no live coding challenge that can accomplish this
My Utopia
What I would like is to be able to send a take home and then have a talk about the implemented solution.
It would be fun to give an input and ask the candidate to parallelize the execution of the input. Have them locate dependencies and get the proper execution order...
Anyhow, if you want to speak with me you can always hit me up in mastodon at fsan at the tilde dot zone server.