Google Interview Question for Software Engineer / Developers


Country: United States




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

summ[0]=first maximum,
summ[1]=first minimum,
summ[2]=second maximum,
summ[3]=second minimum..
.now these could be found using some conditional statements that would run for O(1) time and also since the number of variables are constant the space complexity is O(1)...now u can apply the formula to find the value

- Arunkumar Somalinga October 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But for finding the first maximum and first minimum also we need to traverse through all elements of the array right which leads to O(N). Could you explain more with some code. Please.

- TapeRecordia October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@TapeRecordia, Arunkumar's solution is right. We are basically arranging the values in ascending order in such a way that every element in the newly formed list is interleaved with first maximum, second maximum, and so on. One issue to consider is whether the original data is all positive or not. If not, then we can do the following:

val1=max(max(w,x),max(y,z));
val2=min(main(w,x),min(y,z));
s1=w+x+y+z;
s2=val1+val2;
diff=s1-s2;
val4=diff-val1;
val3=s1-(val1+val2+val4);

sum[0]=val1;
sum[1]=val2;
sum[2]=val3;
sum[3]=val4;

However, if the data is all positive we need to check the condition for val3 and val4 one by one since the summation and the difference may not provide any useful information, as in the case data=5,1,6,2. Here val1=6, val2=1, s1=14, s2=7, and diff =7. So if we use the aforementioned code we get val4=1, val4=6 which is incorrect.

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

What O(n)? Ridiculous. Post the proper question.

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

Where did you study all these...you are genius

- TapeRecordia October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

summ[0]=second maximum,
summ[1]=first minimum,
summ[2]=first maximum,
summ[3]=second minimum..

- Zoro October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's still not O(1). The max and min functions take O(logn) at best.

- A Non October 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A Non and TapeRecordia are both idiots.

- Anonymous October 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry people, I am unclear about this. Could you please provide a running code?

- chandeepsingh85 October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

this is incorrect.

think of {10, 3, 2, 1}. the above proposed solution gives {10, 1, 3, 2}, yielding a result of 9+2+1=12.

the actual maximizing permutation is {2, 10 , 1, 3}, yileing a result of 8+9+2=19.

- Anonymous October 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

zoro is correct.

suppose that w>x>y>z, the above proposed solution {w, z, x, y} gives a value |w-z| + |z-x| + |x-y| = w-x+x-z+x-y = w+x-z-y

then the maximizing permutation should be {y, w, z, x} or {x, z, w, y}, yielding |y-w|+|w-z|+|z-x| = w-y+w-z+x-z = 2w+x-y-2z > w+x-z-y.

- xietangren October 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the given sample input, the permutation suggested by ArunKumar, {5, -1, 5, 3}, gives 6 + |-6 + 2| = 10 but the permutation {5, 3, -1, 5} gives 2 + |4 + 6| = 12.

Am I missing something here?

Since the input is guaranteed to be of length 4 at all times, why is the brute force 4! recursive approach wrong? It's still O(1) time and space.

- pv November 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How is the solution to this supposed to run in O(1) ? Are you sure it's not O(n) time and O(1) space?

- Anonymous October 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A techincally inaccurate explanation to grandma type folks: "When n = 4, O(n) is O(1)".

The real idiocy, though, is requiring O(1) time and space! There are no variables here!

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

Let’s say, we have an array that has w,x,y,z by sorted.
a[0] will be smallest element in w,x,y,z. a[3] will be maximun.
The answer is
summ[0] = a[3]
summ[1] = a[0]
summ[2] = a[2]
summ[3] = a[1]

public int answer(int w, int x, int y, int z){ 
	int[] a = new int[4];
	a[0] = w;
	a[1] = x;
	a[2] = y;
	a[3] = z;
	Arrays.sort(a);
	return Math.abs(a[3] - a[0]) + Math.abs(a[0] - a[2]) + Math.abs(a[2] - a[1]);
}

Note I don't think sorting a is O(n), because 4 is the constant.
if this sorting means O(n), then func(summ) should be O(n) thus we can't solve
this problem.

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

///public class MaxValue {

public static void main(String[] args) {
MaxValue maxValue = new MaxValue();
maxValue.maxValue(7,7,1,4);
}

public int maxValue(int a,int b,int c,int d){

int max = 0;
int min = 0;
//a=2.b=7,c=1,d=4
max = Math.max(Math.max(a,b),Math.max(c,d));//7
min = Math.min(Math.min(a,b),Math.min(c,d));//1

int sum = a+b+c+d;//14

int aPlusB = max+min;
int cPlusD = sum - aPlusB;
int v4 = Math.max(Math.abs(cPlusD - max),Math.abs(cPlusD-aPlusB));
int v3 = sum - (aPlusB+v4);

System.out.println("a "+max);
System.out.println("b "+min);
System.out.println("c "+v3);
System.out.println("d "+v4);

return 0;

}
}\\\

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

summ[0]=second min,
summ[1]=first max,
summ[2]=first min,
summ[3]=second max..

- BIN December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it's sorted ascending, it should be a[2] a[4] a[1] a[3], or reverse it.
We can compare all the result.

- sakar2003 January 09, 2014 | 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