Goldman Sachs Interview Question for Software Engineer / Developers


Country: United States




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

I think what you can do is make a groups in array like if the size of array is 100 make 10 groups of 1-10 11-20 and so on. add 1-10 and compare them to sum of 1-10 from another array. similarly do for all other if the sum is not same you will left with small number of integers to compare. .

- Shahid March 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

Two ways I can think of:
1) If you know that the numbers lie between 1 and some other known number, and if none of the numbers are repeated, then the sum of all those numbers should be n(n+1)/2 n being the last number.
Subtract this from the sum of the all elements in the array and you should get the number that is missing or repeated once.

2) Another way to do this is by using a bit-map vector. Algorithmically speaking, allocate a millions bits of space (10,00,000/8 bytes (10,00,000/8=char's)) and set to 0. For every number encountered in the input array, set the corresponding bit in the bit-map vector. Then do a linear scan through this bit-map vector and look for which entry is at 0, the corresponding number in the array is missing.

This method wont work if a number is repeated rather than missing in the input array.
Time complexity is O(n) and space complexity is excellent i.e- O(n) for n input array elements in bits.

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

For your first comment.: Sum of continuous numbers is n(n+1)/2.
say array is 1,2,7,5,15,21.The sum cant be found by above formula

- Abhi March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Number is not missing but we have replaced the number with a different number, Hence above method wont work

- DashDash May 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Assuming integer 'a' is replaced with integer 'b'
Before replacement :-
sum of remaining integers + a = total sum (say c) -- eq 1
'xor of remaining integers' xor a = total xor (say d) -- eq 2

After replacement :-
sum of remaining integers + b = total sum (say e) -- eq 3
'xor of remaining integers' xor b = total xor (say f) -- eq 4

eq 1 substract eq 3 :-
a-b = c-e

eq 2 xor eq 4 :-
a xor b = d xor f

two variable, two equation. Hence problem solved

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

Could you pls explain how will you find a and b if you have a-b and a XOR b?

- srinu.5019 March 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

forgive me but I didn't quite understand the question.... anyone could give an example on a small array and elaborate the problem? Thank you much!

- penny June 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

prevention is the best medicine :)

GSreplace(unsigned int index, int value)
  {
  UpdateLastReplaced(index);
  GSarray[index]= value;
  }

- __tohotom May 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Should be binary search add all the elements of the first half of the two list compare it if it equal do the second part recursively the same else do first part recursively the same. this will end up with a complexity of O(log(n))

- prasath June 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use the raid level concept...easy one in O(n)

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

Hey Coder, simple and elegant solution. Good one.
a slight modification - it may not change worst case scenario, but considering that there are a MILLION iterations, worth adding -
For i = 1 to MILLION
{ set A[i] = A'[i] - A[i]
if (A[i] != 0)
break;
}

- Anonymous January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If i have the Previous array with me say A[MILLION] and the new array is A'[]

then i can apply the following method:

For i = 1 to MILLION
{ set A[i] = A'[i] - A[i]
}

now scan through the array A[] , the only non zero entry will indicate the position of the changed element. Adding this to A'[i] will give you the original element as well.

complexity : N

- coder! August 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hey Coder, simple and elegant solution. Good one.
a slight modification - it may not change worst case scenario, but considering that there are a MILLION iterations, worth adding -
For i = 1 to MILLION
{ set A[i] = A'[i] - A[i]
if (A[i] != 0)
break;
}

- Anonymous January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

*O(N)

- coder! August 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Coder you rock

- Admirer of Coder January 23, 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