Projects > Codility Problems

Here is another project to improve my programming skills. Codility is a software company that specializes in code testing. That is, they provide problems for which you have to write code to solve, after you submit your code, they analyze it and grade it for you.

Currently they have three demo exercise which can be attempted by anyone. There are also monthly coding challenges that are quite difficult but reward the person who can solve the challenge flawlessly with a certificate. I will attempt those too and post the certificates when i get them!

Please note that the main reason I am doing this is to learn! If you find any mistake in my solution, please don't hesitate to contact me.

I will solve the problems in more than one programming language, initially though, i will start with Python 3. Furthermore, i will post the evaluation report provided by Codility for each.

Please choose the problem from the box below:

Problem Description:

Demo test on Codility

A non-empty zero-indexed array A consisting of N integers is given. The first covering prefix of array A is the smallest integer P such that 0 ≤ P < N and such that every value that occurs in array A also occurs in sequence A[0], A[1], ..., A[P].

For example, the first covering prefix of the following 5−element array A:

	A[0] = 2  A[1] = 2  A[2] = 1
	A[3] = 0  A[4] = 1

is 3, because sequence [ A[0], A[1], A[2], A[3] ] equal to [2, 2, 1, 0], contains all values that occur in array A.

Write a function

def solution(A)

that, given a zero-indexed non-empty array A consisting of N integers, returns the first covering prefix of A.

Assume that:

  • N is an integer within the range [1..1,000,000]
  • each element of array A is an integer within the range [0..N−1].

For example, given array A such that

	A[0] = 2  A[1] = 2  A[2] = 1
	A[3] = 0  A[4] = 1

the function should return 3, as explained above.

Complexity:

  • expected worst-case time complexity is O(N).
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

Code:


def solution(A):
    """Finds the first covering prefix of a list of integers
 
    Given a zero-indexed non-empty list A consisting of N integers, returns the
    first covering prefix of A.
 
    """
 
    input_set = set(A)
 
    #Counter to keep track of index being processed
    counter = 0
 
    while input_set:
        if A[counter] in input_set:
            input_set.remove(A[counter])
 
        counter += 1
 
    return counter-1
    

Codility Evaluation: