A Codility-type question walkthrough

and why doing these questions are worth your time

If you are like me, a noob and entry-level data scientist, you probably have sighed at the thought of the dreaded coding challenge. There are so many possible questions, how could you know what will be on the test? Data science and analytics knowledge is one thing. But being able to prove your skills in your language of choice (mine is Python) is something impossible to avoid.

Think from the hiring manager’s perspective. They see you might have some projects and coding experience, but that doesn’t completely cut it for them. As one respectable senior data scientist once told me, who will the hiring person choose: the one who can code the right answers after scouring through stack overflow and google, or the one who can code the answer just from the knowledge in his head? I think you know the answer to that.

These questions might seem mindless and unreasonably challenging at times, but your answers to them demonstrate the skills in your head. And once you start to get the right answers, you will feel good. How you approach these problems is also key to successfully completing them.

Here is a question on a code challenge site that is a good example of something you might see:

We have an array, A, with N different integers in it. The integers in the arrary have a range of (1, (N+1)). Since there are only N integers, that extra one is missing. Return that missing element using a function.

For example: array A = 2, 3, 4, 5

We want to return 1, the missing element.

To start off, it helps to code an input so you can test different scenarios:

#returns an open input, where you can type space-separates values and coerce them into a list of ints:A = list(map(int, input().split()))#2 3 4 5

If we break down this problem step by step, it is much easier than you think.

We know the length of A is N, and also that the range of the array A is N + 1:

n = len(A)
n + 1
#5

If we think of adding up (sum) the numbers in this array, we can get a good idea of what is in it. So let’s multiply this side, plus 1 more, as if we had all the numbers in succession (1, 2, 3, 4, 5).

n+2#6

Now add these two up and divide by two. This will give us the sum of all numbers (none missing):

5*6/2#15

Now if we sum the actual input array, A, we get our sum with the number missing:

sum(A)#14

If we subtract the full range sum by the actual array sum, we get out number!

(5*6/2) - sum(A)#1

Now we have to compact this process into one function:

def solution(A):
n = len(A)
total = (n + 1)*(n + 2)/2
return int(total - sum(A))
solution(A)

That’s it. See how walking through the logic step-by-step was making this much easier? This is my general approach for all of these types of questions. The more you do this, the more fluid your capacity to answer questions using Python becomes!

I’m a recent Data Science graduate with a B.S. in Environmental Science. Currently seeking job opportunities. Constantly learning!