Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming Test - Codility - Dominator [closed]

Tags:

java

I just had a codility problem give me a hard time and I'm still trying to figure out how the space and time complexity constraints could have been met.

The problem is as follows: A dominant member in the array is one that occupies over half the positions in the array, for example:

{3, 67, 23, 67, 67}

67 is a dominant member because it appears in the array in 3/5 (>50%) positions.

Now, you are expected to provide a method that takes in an array and returns an index of the dominant member if one exists and -1 if there is none.

Easy, right? Well, I could have solved the problem handily if it were not for the following constraints:

  • Expected time complexity is O(n)
  • Expected space complexity is O(1)

I can see how you could solve this for O(n) time with O(n) space complexities as well as O(n^2) time with O(1) space complexities, but not one that meets both O(n) time and O(1) space.

I would really appreciate seeing a solution to this problem. Don't worry, the deadline has passed a few hours ago (I only had 30 minutes), so I'm not trying to cheat. Thanks.

like image 888
Matthias Avatar asked Sep 14 '12 02:09

Matthias


People also ask

Can we switch tabs in Codility test?

You have several tabs you can switch between: Summary (overall summary of the session), Details (candidate and session details), Timeline (time used and user notes).

Is there camera in Codility test?

When activated, the candidate will have to scan a photo ID document and take a selfie. Using facial recognition and AI, our solution will validate if the candidate starting the assessment can identify themselves using a legit ID document.

Is Codility test recorded?

Codility Usually records the whole history, including runs and corrections. This does NOT mean you have to be perfect- the opposite is true. Mistakes tell the reviewer about your ability to work your way to the solution.


2 Answers

Googled "computing dominant member of array", it was the first result. See the algorithm described on page 3.

element x; int count ← 0; For(i = 0 to n − 1) {   if(count == 0) { x ← A[i]; count++; }   else if (A[i] == x) count++;   else count−−; } Check if x is dominant element by scanning array A 

Basically observe that if you find two different elements in the array, you can remove them both without changing the dominant element on the remainder. This code just keeps tossing out pairs of different elements, keeping track of the number of times it has seen the single remaining unpaired element.

like image 143
Keith Randall Avatar answered Sep 19 '22 00:09

Keith Randall


Find the median with BFPRT, aka median of medians (O(N) time, O(1) space). Then scan through the array -- if one number dominates, the median will be equal to that number. Walk through the array and count the number of instances of that number. If it's over half the array, it's the dominator. Otherwise, there is no dominator.

like image 43
Jerry Coffin Avatar answered Sep 20 '22 00:09

Jerry Coffin