Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

review of a codility test - pair_sum_even_count

I recently took an online test on codility as part of a recruitment process. I was given two simple problems to solve in 1 hour. For those who don't know codility, its an online coding test site where you can solve ACM style problems in many different languages.

If you have 30 or so mins then check this http://codility.com/demo/run/

My weapon of choice is usually Java.

So, one of the problems I have is as follows (I will try to remember, should have taken a screenshot)

Lets say you have array A[0]=1 A[1]=-1 ....A[n]=x

Then what would be the smartest way to find out the number of times when A[i]+A[j] is even where i < j

So if we have {1,2,3,4,5} we have 1+3 1+5 2+4 3+5 = 4 pairs which are even

The code I wrote was some thing along the lines

int sum=0;
for(int i=0;i<A.length-1;i++){
 for (int j=i+1;j<A.length;j++){
   if( ((A[i]+A[j])%2) == 0 && i<j) {
       sum++;
    }
  }
}

There was one more restriction that if the number of pairs is greater than 1e9 then it should retrun -1, but lets forget it.

Can you suggest a better solution for this. The number of elements won't exceed 1e9 in normal cases.

I think I got 27 points deducted for the above code (ie it's not perfect). Codility gives out a detailed assessment of what went wrong, I don't have that right now.

like image 472
geoaxis Avatar asked Jan 16 '11 00:01

geoaxis


People also ask

How difficult are Codility tests?

The difficulty level of the Codility test depends on the company you're interviewing for, your level of experience and the specific role you're applying for. The main challenges here are: Working under time pressure. Not being able to tell the interviewer your thought process directly.

What is a good score in Codility test?

The best way to ensure success during the application process is to score 100% on the Codility test. If you score between 60% to 99% you may still be successful, but your score will be subject to a review from a Microsoft recruiter. If you score under 60% then you will fail the Codility test.

Is Codility assessment good?

Pros: A great coding platform, it's easy to use with a clean interface. I've enjoyed the exercises available. The problem statement and examples have great clarity without being too wordy or short. I've taken a couple of tests for companies who used Codility for screening.

What are Codility tests like?

Codility is a coding test platform used by many recruiters. The setup is similar to LeetCode, with the question given on the left side of the screen and the code editor on the right side. As with LeetCode, you're able to run your code using your own test cases and print out to the console.


2 Answers

The sum of two integers is even if and only if they are either both even or both odd. You can simply go through the array and count evens and odds. The number of possibilities to combine k numbers from a set of size N is N! / ((N - k)! · k!). You just need to put the number of evens/odds as N and 2 as k. For this, the above simplifies to (N · (N - 1)) / 2. All the condition i < j does is to specify that each combination counts only once.

like image 82
Svante Avatar answered Oct 19 '22 03:10

Svante


It's very simple First you need to find the number of odds and even number in collection. eg. x is odd if x&1 ==1, even otherwise, if you have this, knowing that adding two even or two odds to each you get even. You need to calc the sum of Combinations of two elements from Even numbers and Odd numbers.

having int A[] = {1,2,3,4,5};

int odds=0, evens=0;
for (int i=0; i< A.length; ++i)
{
if (A[i]&1==1) odds++;
else evens++;
}
return odds*(odds-1)/2 + evens*(evens-1)/2;  

// Above goes from fact that the number of possibilities to combine k numbers from a set of size N is N! / ((N - k)! · k!). For k=2 this simplifies to (N · (N - 1)) / 2

like image 45
Slawomir Karczewski Avatar answered Oct 19 '22 01:10

Slawomir Karczewski