Goldman Sachs Interview Question for Software Engineer / Developers






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

//returns -1 if not found
int maxRepeatedElement(int arr[], int size)
{
   if( size <= 0)
      return -1;

   int count = 0;
   int elem;

   for (int i = 0; i < size; ++i)
   {
      if (count == 0)
      {
         elem = arr[i];
         count = 1;
      }
      else if (elem == arr[i])
      {
         ++count;
      }
      else
      {
         --count;
      }
   }
   
   count = 0;
   for(int i = 0; i < size; ++i)
   {
      if (arr[i] == elem)
      {
         ++count;
      }
   }

   if (count > size/2)
   {
      return elem;
   }

   return -1;
}

- gevorgk June 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,

This is pretty good. But was wondering is it possible to solve it in the space complexity of O(1) as now its O(2).

-Pallav

- gurha.pallav June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

You make me cry :))))
O(1) means space which doesn't depend on input size.
And I don't know what is O(2) :))))

- gevorgk June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

abe gadhe gevorgk tera logic nai samajh aaya...

- netappreject June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice language. What about english ?

- gevorgk June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@gevorgk
awesome work man!!!!
don't worry what netappreject has said. when such people don't have any point to make this is what they do...

@netappreject
There is better way to say this....Please be nice to people who are sharing their knowledge and helping you...

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(2) is a nonstandard way to say O(1). Pallav, if using two ints bothers you, just pack them both into a long.

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@gevorgk - Can you also pls explain, your thinking process - how you approach the problem and devise the solution - I assume here you approached it as the number which occurred more than n/2 has to either alternate starting from 1st position (e.g. 1 2 1 4 1 5 1) or has to occur consecutively atleast 1 (e.g. 5 4 1 1 6 1 1)

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the element is repeated more than n/2 times, than it definitely occurs consecutively atleast once...

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I doubt he came up with the algorithm because the code looks very similar to the algorithm in taylor.thursday.com pdf. But if he came up with it great!

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution has a flaw.
If numbers are in this form
1, 20, 2, 20, 3, 20, 4, 20 ...
Then this solution doesn't work.
Because it never get a chance to store 20 in elem.

- Nitin June 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if the code is modified a little bit like mentioned below.
......
else
{
--count;
--i;
}
........
Then things work fine.
Ya But then I don't know what will be the complexity.
In worst case It probably comes out to be O(n+n/2), i.e. O(3n/2), i.e. O(n).
Please comment on it.
Please point out if you see any problem in it or type "CORRECT" if it is correct.

- Nitin

- Nitin June 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Point here is definition of majority is (n/2) + 1. thats way:

1 20 2 20 3 20 4 20 needs to keep another 20, for n/2 it fails several cases.

- netappreject June 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, right.
The solution is correct if 20's are more than n/2.
:-)

- nitin June 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

find the median element using the quick select algo.

- Anonymous May 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Qsort takes O(NlogN). Can you explain your logic.

- cirus June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(Different anonymous) quickSELECT. It's O(n) time but Omega(n) space.

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Array space already given as input is used in 2quick select so we can say that no extra space is used so space complexity is constant.

- hemant June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, OK, but there's a solution that doesn't modify the input.

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

taylor.thursday.com/classes/cs307/2/Majority%20Element.pdf

- Prateek June 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks gevorgk.

- Prateek June 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi gevorgk , as you are setting again count=0 . It making count logic of first for loop useless. I am not getting why that is required. also according to my analysis your logic will be definitely failed for array {3,1,1}

- code bug June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The first loop finds the required element if it exists and an arbitrary element otherwise. The second loop checks whether this element occurs more n/2 times, which is sort of pointless given that the input is guaranteed to have that property.

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's not useless, count logic in first loop guarantees that element with more than n/2 occurrence will survive.
And I don't see any problem with {3, 1, 1}.

3 - elem = 3, count = 1
1 - elem = 3, count = 0
1 - elem = 1, count = 1

So we have elem = 1 after first loop and check if it occures more than n/2 in second loop.

- Anonymous June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution

- nvguest June 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution gevorgk

- nvguest June 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey87435" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}

</pre><pre title="CodeMonkey87435" input="yes">1
2
10
42
11

</pre>

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

The algorithm, though very clever, does not work in all cases. For example:
1112222111

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

very gud solution , great work !!

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

#include<stdio.h>
#define arraysize 6
int array[arraysize]={1,3,3,3,4,3};

int repeatedElem(int array[], int size)
{

int index = 0;
int count = 0;
int val;
int elem;
count = 0;
for(index=0;index<size;++index)
{
elem = array[index];
if(count ==0)
{
val = elem;count++;

}
else if(val == elem)
{
count++;
}
else
{ count--;}
}
if(count>0)
{
return val;
}
else{return -1;}

}

int main()
{
int ans = repeatedElem(array, arraysize);
printf("answer is %d ", ans);
return 0;
}

- deepak June 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Try this {10,2,3,10,2,2,2,2,4,4,4,5,5}
Your answer is '5'. Doesn't work

- chennavarri October 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But there is no such element which is occurring n/2 times.
Total elements: 13
occurrence of 2: 5.

But this input won't work.
{10,2,2,3,10,2,2,2,2,4,4,4,5,5,2,2}

Total elements: 16
occurrence of 2: 8
Result will be 5.

- Piyush November 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

gevorgk: gorgous solution!

- chenming831 June 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

traverse the array from i=0 to n-3
if a[i]==a[i+1] or a[i]==a[i+2]
then num=a[i]

it's simple

- bond July 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I found Bond's solution the best.It works and i'm not that good with calculating complexity but I m sure there is only one for loop so complexity must be less.

- anjum August 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think the second loop is necessary since the first loop is done under the assumption that there is a number existing for more than n/2. therefore, the survivor in first loop is the one. Without this assumption, the first loop fails. for example:1,1,2,2,3

- Anonymous September 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nice solution gevorgk .
I would like to comment for second loop.
First loop will at least give us most occurred element in the array. (As you can see ppl have posted sequence in which algo doesn't work).
Second for loop is just for the confirmation that we have chosen the correct element, if ele given by first loop is not occurring n/2 times then no element is there.
Hope it make sense.

- Piyush November 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry,first loop doesn't give most occurred element. My mistake. I think second loop is not required. I tried running this program on machine. works for all the inputs I gave.

- Piyush November 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Awesome

- Ank February 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Gorgeous!!!!

- byakuya June 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

awesome work!! keep goin!!

- Anonymous July 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks guys :)

- gevorgk August 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int maxRepeatedDigit(int []A){

for(int i=0;i<=A.length/2;i++){
if(A[i]==A[A.length-i-1]){
if(countData(A[i],A)> A.length/2)
return A[i];
}
}

return -9999999;

}

public int countData(int x, int []A){
int count=0;
for(int i=0; i<A.length;i++){
if(x==A[i])
count++;
}
return count;
}

- Dr. Scott September 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming no other numbers are repeated except the one N. N will consecutively repeat itself at least once.  complexity between O(1) - O(n) with 1 var.

int findRepeaterNum(int *arr)
{
    int *i=arr+1;
    while(*arr++!=*i++);
    return *i;
}

- Anonymous March 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

actually the return is> return *arr;

- Anonymous March 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Add all element in the array in binary tree havin a member frq in the tree increment the frequency when the same elemnt is added in the tree again . traverse through the tree and and element which frequency is more than n/2..

- Anonymous March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is Boyer && Moore's the majority vote algorithm, you can find the description at ht tp:// ww w.cs.utexas.edu/~moore/best-ideas/mjrty/.

- ashot madatyan July 26, 2013 | 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