Yahoo Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

Note that if there exist a majority element in an array then that element must occur at A[n/2](convince yourself). So, if we can find element with rank n/2(say 'X') in the input array we can just scan the array to count the no .of times 'X' appears in it. If this count is greater than n/2 then it's the majority element else there's no majority element. It will be O(N). But the main problem is how do we find X??

For that there exists an O(N) time algorithm to find the element with a given rank in an array(Google for "linear time order statistic" or refer to CLRS).

- cool September 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is one of the solution, there is another simplier solution called "majority algorithm". It's quite simple the trick is if there exist a majority element, it will always be the candidate that return by the method "FindMajorityCandidate", in this case, we just need to look through the list again to see if that's a true Majority element or not (same as the second step mentioned above)


public int FindMajority(int* arr, int n)
{
int majorityCandidate = FindMajorityCandidate(arr,n);
// count whether the candidate is a real majority element;
int count = 0;
for(int i=0; i<n ;i++)
{
if( arr[i]==majorityCandidate )
{
count++;

}
}
if(count>n/2)
{
return majorityCandidate;
}
return -inf;
}

public int FindMajorityCandidate(int* arr, int n)
{
int majority;
int count = 0;

for(int i=0; i< n; i++)
{
if(majority == arr[i])
{
count++;
}else if(--count <= 0)
{
// change the majority candidate when the count for the current candidate become 0;
majority = arr[i];
count = 1;
}
}
}

- Nat September 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is one of the solution, there is another simplier solution called "majority algorithm". It's quite simple the trick is if there exist a majority element, it will always be the candidate that return by the method "FindMajorityCandidate", in this case, we just need to look through the list again to see if that's a true Majority element or not (same as the second step mentioned above)


public int FindMajority(int* arr, int n)
{
int majorityCandidate = FindMajorityCandidate(arr,n);
// count whether the candidate is a real majority element;
int count = 0;
for(int i=0; i<n ;i++)
{
if( arr[i]==majorityCandidate )
{
count++;

}
}
if(count>n/2)
{
return majorityCandidate;
}
return -inf;
}

public int FindMajorityCandidate(int* arr, int n)
{
int majority;
int count = 0;

for(int i=0; i< n; i++)
{
if(majority == arr[i])
{
count++;
}else if(--count <= 0)
{
// change the majority candidate when the count for the current candidate become 0;
majority = arr[i];
count = 1;
}
}
}

- Nat September 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dude @cool..given that the elements may not be integers too in that the greater or less than comparison will not be possible..is it via ascii comparison????

- dinesh March 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A minor mistake in your code...

if(count>n/2)
{
return majorityCandidate;
}

should be

if (count >= ((n-1) / 2) +1 ) {
return majorityCandidate;
}

considering the odd and even number of elements.

- Delon August 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@cool: Ur solution is not convincing ,coz there mi8 be a possibility that majority element's all the occurrences are in first and last quarter of array and some other element at A[n/2].. so it need not be necessary that majority element if accurs will be surely at A[n/2] position....

- saumils1987 September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<blockquote><code><font size="2" face="Courier New" color="black"><font color="#0000ff">int</font> getit(<font color="#0000ff">int</font> *ar, <font color="#0000ff">int</font> num)
{
&nbsp;&nbsp;<font color="#0000ff">int</font> full_pairs = (num + 1) / 2;
&nbsp;&nbsp;<font color="#0000ff">bool</font> odd_num_of_pairs = full_pairs % 2 ? <font color="#0000ff">true</font> : <font color="#0000ff">false</font>;
&nbsp;&nbsp;<font color="#0000ff">int</font> midle_index = num &#62;&#62; 1;
&nbsp;&nbsp;<font color="#0000ff">int</font> midle_item = ar[midle_index];
&nbsp;&nbsp;<font color="#0000ff">bool</font> left(<font color="#0000ff">false</font>);
&nbsp;&nbsp;
&nbsp;&nbsp;<font color="#0000ff">if</font>(ar[midle_index - 1] == midle_item)
&nbsp;&nbsp;&nbsp;&nbsp;left = <font color="#0000ff">false</font>;
&nbsp;&nbsp;<font color="#0000ff">else</font> <font color="#0000ff">if</font>(ar[midle_index + 1] == midle_item)
&nbsp;&nbsp;&nbsp;&nbsp;left = <font color="#0000ff">true</font>;
&nbsp;&nbsp;<font color="#0000ff">else</font> <font color="#0000ff">return</font> midle_item;

&nbsp;&nbsp;<font color="#0000ff">if</font>((left &#38;&#38; odd_num_of_pairs) || (!left &#38;&#38; !odd_num_of_pairs))
&nbsp;&nbsp;&nbsp;&nbsp;getit(ar + midle_index, num - midle_index);
&nbsp;&nbsp;<font color="#0000ff">else</font> getit(ar, midle_index);

}

<font color="#0000ff">int</font> main()
{
&nbsp;&nbsp;<font color="#0000ff">const</font> <font color="#0000ff">int</font> N = 3;
&nbsp;&nbsp;<font color="#0000ff">int</font> ar[N] = {1, 2, 2};<font color="#008000">//, 2, 3, 3, 4, 4, 5, 6, 6};&nbsp;&nbsp;</font>
&nbsp;&nbsp;std::cout &#60;&#60; getit(ar, N);
}</font>

</blockquote>

- Anonymous October 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<blockquote><code><font size="2" face="Courier New" color="black"><font color="#0000ff">int</font> getit(<font color="#0000ff">int</font> *ar, <font color="#0000ff">int</font> num)
{
&nbsp;&nbsp;<font color="#0000ff">int</font> full_pairs = (num + 1) / 2;
&nbsp;&nbsp;<font color="#0000ff">bool</font> odd_num_of_pairs = full_pairs % 2 ? <font color="#0000ff">true</font> : <font color="#0000ff">false</font>;
&nbsp;&nbsp;<font color="#0000ff">int</font> midle_index = num &#62;&#62; 1;
&nbsp;&nbsp;<font color="#0000ff">int</font> midle_item = ar[midle_index];
&nbsp;&nbsp;<font color="#0000ff">bool</font> left(<font color="#0000ff">false</font>);
&nbsp;&nbsp;
&nbsp;&nbsp;<font color="#0000ff">if</font>(ar[midle_index - 1] == midle_item)
&nbsp;&nbsp;&nbsp;&nbsp;left = <font color="#0000ff">false</font>;
&nbsp;&nbsp;<font color="#0000ff">else</font> <font color="#0000ff">if</font>(ar[midle_index + 1] == midle_item)
&nbsp;&nbsp;&nbsp;&nbsp;left = <font color="#0000ff">true</font>;
&nbsp;&nbsp;<font color="#0000ff">else</font> <font color="#0000ff">return</font> midle_item;

&nbsp;&nbsp;<font color="#0000ff">if</font>((left &#38;&#38; odd_num_of_pairs) || (!left &#38;&#38; !odd_num_of_pairs))
&nbsp;&nbsp;&nbsp;&nbsp;getit(ar + midle_index, num - midle_index);
&nbsp;&nbsp;<font color="#0000ff">else</font> getit(ar, midle_index);

}

<font color="#0000ff">int</font> main()
{
&nbsp;&nbsp;<font color="#0000ff">const</font> <font color="#0000ff">int</font> N = 3;
&nbsp;&nbsp;<font color="#0000ff">int</font> ar[N] = {1, 2, 2};<font color="#008000">//, 2, 3, 3, 4, 4, 5, 6, 6};&nbsp;&nbsp;</font>
&nbsp;&nbsp;std::cout &#60;&#60; getit(ar, N);
}</font>

</code></blockquote>

- Anonymous October 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Hash Table. Start from index 0 and hash the elements one by one.
When ever u change the count of a element, check whether the new count is greater than n/2 or not. If yes, then this is your majority element.
Time Complexity O(n).

- neeraj.fullwithattitude September 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if we remove 2 different elements from array say a and b and a != b then ans of remaining element i.e ans of array of size N-2 will be same as ans of N element array.
we can do this using one counter. and one variable.

int majority(int *a){
     temp ;
     count = 0;
     for( int i = 0; i< N; i++){
           if( count == 0) temp = a[i];
            if( a[i] == temp)
                    count++;
           else{
                    count--;
          }
         
   }
   if(count > 0) return temp;

}

- jigs February 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This would fail for array 3,2,2,3,2,3,2,3,5. Algo would give 5 as majority element.
To complete your solution, I would take the projected solution from Algo (5 in this case) and verify that frequency of 5 is more than 9/2. If not, there is no number in majority.

- saket September 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If number is in majority then at least two of them are adjacent. Therefore using a[i]=a[i+1] we can find the majority number in O(n).

- vivs0766 July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@vivs,

your algo will fail for this test case : 6 1 6 2 6 3 6

- Anonymous October 29, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More