How to Read the Candidate’s Code


When you are evaluating an interview, there are many things you have to consider. One of the most important parts is the coding. The code of the candidate is like an open book, but you have to learn how to read it. Even a few lines of code can help you get a better understanding of the candidate’s experience and technical level.

I want to show you how can you use a very simple question to reveal the real knowledge of the candidate. I am going to share with you some answers provided by real candidates, and you will see how to analyse their answers.

The Question

First let’s see the question:

Reverse the letters of the input words. Each line is a list of words and contains only letters of the English alphabet and one space between the words. Read the input from the standard input.

Something like this:

Input:

word1 word2 word3
another sentence

The output should be:

1drow 2drow 3drow
rehtona ecnetnes

I think everybody agrees this is a very simple task. A senior developer could solve it in about one to five minutes, depending on the programming language.

It was one of the questions on the online test that was mandatory for the candidates if they wanted to apply for the job. The InterviewMe.io automatically tested the code for different inputs and provided the execution time. From that, I could see the time that the candidate spent solving the problem. All of these parameters will help you to make your decision.

Before I show you some real life answers, let me clarify something. I don’t want to make fun of any candidates. I am sharing this information to help you conduct interviews more efficiently. Or if you are a developer, you can use this code to learn and improve yourself.

1. Example: The Senior

Let’s start with a good solution provided by a senior developer. The first good sign was the time that the candidate spent to finish the test. It took him only three minutes to finish. Take a look at the code:

<?php
//This code read the necessary data from the standard input
while ($line = trim(fgets(STDIN))) {
//$line contains the line of the input
$array = explode(' ', $line);
$array = array_map('strrev', $array);
$result = implode(' ', $array);
echo $result . "\n";
}

It is easy to understand with just a few lines of code. Let me draw your attention to the smart usage of the built-in functions that make the code fast. It is a smart solution. Of course, this doesn’t mean you should instantly hire the candidate, but he/she probably has the basic analytical skill and experience in the given programming language.

2. Example: The Junior

<?php

function reverse_string($str) {
$lenght = strlen($str);
$limit = floor($lenght / 2);
for ($i = 0;$i < $limit;$i++) {
$tmp = $str[$i];
$str[$i] = $str[$lenght - ($i + 1)];
$str[$lenght - ($i + 1)] = $tmp;
}
return $str;
}

while ($line = trim(fgets(STDIN))) {
//$line contains the line of the input
$len = strlen($line);
$new_line ='';
$word = '';
for($i = 0;$i < $len;$i++) {
if ($line[$i] == ' ') {
$new_line = $new_line . ' ' . reverse_string($word);
$word ='';
} else {
$word .= $line[$i];
}
}
$new_line = $new_line . ' ' . reverse_string($word);
echo $new_line . "\n";
}

This code has a higher complexity but is still easy to understand; the names of the variables are descriptive, and you can see some kind of coding standard too. Usually, junior developers provide a solution like this because they have no experience how to use the built-in functions.

3. Example: Trust me, I am an (over)engineer.

This is one of the most memorable interviews of my life. I can clearly remember this candidate. Let’s take a look at the code. This code is huge, so I prefer to share it here:

http://interviewpad.io/p/I_know_oo

Your first thought might be, “This code looks nice.” And you might be right. This code is carefully documented, easy to read, and well organised. So what is wrong with it?

First, try to understand how it works. Let me help you. It reverses the whole string, and after that it reverses the string again but word by word. If you couldn’t understand it, it is not your fault. The logic is carefully hidden behind the OO structure.

Second, why OO? This is a very simple task, and as you can see, the good solution is no more than five lines of code. There is no reason to use objects to solve this problem, and actually it is not OO at all. There are no private variables, no inheritance; nothing is OO. It simply consists of just some function wrapped inside a class. The above code is a perfect example of the functional programming with classes.

Fourth, slow execution time. Even though the output of his code was correct, my colleague from HR was suspicious because this code was slower than the average (much slower). As she isn’t a developer, she asked me to have a look at the code. I want to be sure our candidate is an over engineer, so I picked up the phone to call the candidate.

I used my favorite over engineer detector question:

“Our website uses MVC framework, and we have around 10,000 views. How can you find the views that contain the address of the office? You have access to the server and the source code.”

After this, he provided me the most extreme answer that I ever heard:

“Use regexp to detect the phone numbers.”

Ok, that’s fine, I thought.

But after that he continued, “. . . in JavaScript.”

“Do you want to use Node.js?” I asked

“No, on client side. Use AJAX to get the content and call all the actions.”

“And how do you know the name of the controllers and actions?”

“To get the actions, I can parse the controllers.”

It was enough for me to finish the interview, but before that I kindly acknowledged the answer and explained him maybe using grep or any server side script with regexp to read the view files directly would be a better option.

Over engineering is dangerous. The more complicated the code, the more chance there is to make mistakes. It increases the number of the special cases and reduces the readability of the code.

4. Example: I don’t know what standard input is.

Another popular mistake is when the candidates don’t know how to deal with the standard input. Some candidates get confused, and they just ignore this part and instead use a hardcoded string like this:

<?php
$str = "Hello World

lorem ipsum";
$myArray = str_word_count($str, 1);
foreach ($myArray as &$a) {
$a = strrev($a);
}

foreach ($myArray as $rev) {
if ($rev == 'rb') {
echo " ";
}else {
echo $rev." ";
}
}

However, code like this has a really low score on InterviewMe, so it’s easy to filter them out; they are close to the good solution. Everything is fine except the missing standard input. Because of this, I gave a chance for these candidates who “forget” to use the standard input. But after many bad experiences, I had to stop this practice, and I recommend the same to you.

Usually, they passed the algorithmical questions but failed the other parts of the interview, like security or web technologies. In general, these candidates has less experience, and they were not confident when using the command line.

5. Example: I cannot solve it.

The next example will show you when even a simple test like reversing the input words can be too difficult. Let me remind you this example is provided by a real candidate who applied to a senior PHP developer position; after 30 minutes of struggling (that was the time limit of the test), he came up with this:

<?php
while ($arrValue= trim(fgets(STDIN))) {
if(!empty($arrValue)){
$t = count($arrValue) - 1;
$j = 0;
foreach($arrValue as $k => $v) {
if(($k > $t) || $t >= 100000) {
break;
}
if(strlen($v) <= 10000) {
$v = preg_replace('#(\s+)$#imsU', '', $v);
$v = preg_replace('#[^A-z\s]+#imsU', '', $v);
echo $result = ($j != 0)?"\n" . convertString($v):convertString($v);
$j++;
}
}
}
}

function convertString($input){
$output = "";
$lastpos = strlen($input);
for ($i = strlen($input) - 1; $i >= 0; $i--) {
if ($input[$i] == ' ') {
$output .= substr($input, $i + 1, $lastpos - $i -1).' ';
$lastpos = $i;
}
}

$output .= substr($input, 0, $lastpos);
return $output;
}

This “solution” is not just ugly, but it doesn’t work. Now imagine that this candidate wrote a core functionality of your application. Of course, after six months, he will leave because it is too hard for him and what remains is his spaghetti code that nobody can understand.

Fortunately, the interviewme.io’s net caught the fish before wasting anybody’s time.

Conclusion

The shocking fact is that only 10% of the candidates can provide a solution like the first one. 20% of the interviewees create something like the second example, and it is still ok. But the rest— 70%—are unacceptable. And this is just the first, easiest programming question of the test.

The technical interview is the most expensive part of the recruiting process. It requires at least two employees from the technical staff, preparation, paperwork, scheduling the appointments, booking the interview room, and many other things. Don’t let inappropriate candidates waste your resources.