Google Interview Question for Software Engineer in Tests


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 3 vote

The problem is different from careercup.com/question?id=8872057 .
Here the array needs to be divided in two Equal parts not just two parts. Though I think it won't always be possible to find such solution for each input (equal parts with equal average.) But another problem could be try to minimize the difference between average of two equal parts.

- Anonymous October 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Lets see if we can re-phrase the question:
Divide the array into two EQUAL halves which has same AVERAGE

which means, the array is of even size, and the question can be rephrased as:
Divide the array into two EQUAL halves so that they have the same SUM

Algorithm:
1. compute the total sum of the array elements: lets say it is X
2. We need to find two subarrays of equal length which adds to X/2
3. Use subset sum algorithm to see if such subarrays exist

- IntwPrep October 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am sorry but that's what they asked me and if you know the solution just post it or the link to earlier thread rather than abusing me. I think you got it Looser.

- learner October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

careercup.com/question?id=8872057

- anonymous October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thank you

- learner October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Divide the array into two equal halves array1, array2. Say each one has sum S1, S2.
if(S1 > S2)
Then recurse on left array.

(i.e
divide array1 in two equal halves S11, S12.
if(S11 < S22 + S2)
Then recurse on array[n/4... n/2]....
else if (S11 > S22 + S2)
Then recurse on array[0... n/4]
and so on....
)
else if( S1 < S2)
Recurse on right half....

- jackass October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Partition(int a[], int l, int n1, int n2, int k1, int k2)
{
int S1 = sum(a[0 ... l/2 - 1]) + k1;
int S2 = sum(a[l/2 .... l - 1]) + k2;
n1 = l/2 ;
n2 = l/2 ;
if(k1 != 0 && k2 == 0)
n1 += k1;
else if(k2 != 0 && k1 == 0)
n2 += k2;

if(S1 * n1 > S2 * n2)
{
Partition(a, l / 2, 0, n2, 0, S2);
}
else if( S1 * n1 > S2 * n2)
{
Partition(a, l / 2, n1, 0, S1, 0);
}
else
return l/2;
}

// call in main
Partition(a, l, 0, 0, 0, 0);

- jackass October 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I added my solution to original post careercup.com/question?id=8872057 that uses Memoization. Only restriction is that it can't support -ve numbers, needs some alterations to support that.

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

en.wikipedia.org/wiki/Partition_problem

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

this is even harder than the original partition problem because the partition needs to be of equal size

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

Actually, I think the constraint of equal size makes the problem easier. You can partition the problem into two arbitrary halves, and then start exchanging elements. After each exchange, you see if the problem is solved, if not, you recurse on the remaining n/2-1 elements for each side. You could use memoization to prevent repeated computations. I think this avoids an NP complete problem, no?

- wealthychef June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Think of array {3,0,0,1}

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

I agree that is's a old problem. But dont you use f-bomb, ok?

- xzy0410 October 08, 2011 | Flag


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