About Us

Glassdoor is your free inside look at Google interview questions and advice. All interview reviews posted anonymously by Google employees and interview candidates.

Search

for

in

Interview Question for Software Engineer at Google:

Oct 19, 2009

The implementation question: Find a max and min in an array simaltaneously. I used a 2n comparisons approach and a 1.5n on-average approach.

Add Tags [?]
Helpful Question?  
Yes | No
Share: Link Digg

Answers & Comments (6)

Oct 19, 2009

by Interview Candidate:

There is a very common simple approach which does worst case 1.5n (google for this), could not come up with this. The interview ended such that I did not do both the questions anywhere near satisfactorily. So expecting a negative result :(
Helpful Answer?  
Yes | No
Inappropriate?

0 of 1 people found this helpful

Oct 29, 2009

by neakor:

You can use a simple divide and conquer technique in which you divide up the array recursively and only compare when you reach the leaf node, in which case you only have two numbers in your divided array. Then recursively go up to compare with the results from other sub-arrays. This should give you a worst case 1.5n comparison performance.
Helpful Answer?  
Yes | No
Inappropriate?

Nov 7, 2009

by cockroachzl:

Hey neakor,
please do not keep misleading others, divide and conquer will lead u O(nlogn).
Helpful Answer?  
Yes | No
Inappropriate?

Nov 7, 2009

by neakor:

hi cockroachzl:

a complete sorting using divide and conquer will lead to worst case O(nlogn).
however, in this case, the merging steps can be simplified to just two comparisons, thus allowing the worst case performance to be 1.5n. i have the algorithm implemented in java. you can try it out. for the merging step, just check the first and last elements of the 2 sub arrays and do swaps.
Helpful Answer?  
Yes | No
Inappropriate?

Nov 14, 2009

by ellemeno:

I'm confused... why not just:

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < a.length; i++{
    if (a[i] > max) {
        max = a[i];
    }
    else if (a[i] < min)
        min = a[i];
    }
}

Is this the 2n approach? O(2n) = O(n)....

What am I missing?
Helpful Answer?  
Yes | No
Inappropriate?

Nov 14, 2009

by ellemeno:

Sorry... found it. Thank you CLR!

int tempMin, tempMax;
int max = a[0];
int min = a[0];
for (int i = 1; i < n-1; i = i+2){
    if (a[i] <= a[i+1]){
        tempMin = a[i];
        tempMax = a[i+1];
    }
    else {
        tempMin = a[i+1];
        tempMax = a[i];
    }
    if (tempMax > max) {
        max = tempMax;
    }
    if (tempMin < min) {
        min = tempMin;
    }
}

if (n % 2 == 0) { // Do this if there are an even number of elements
if (a[n-1] < min) min = a[n-1];
else if (a[n-1] > max) max = a[n-1];
}
Helpful Answer?  
Yes | No
Inappropriate?
Post your anonymous review or sign in to answer or comment on this question
Google Overview (GOOG )
Web
www.google.com
Industries
Size
5000+ Employees, $21B+ Revenue
HQ
Mountain View, CA
Competitors



Tags are like keywords that help categorize interview questions that have something in common.

Flag this {0} as inappropriate

Would you like us to review something? Please describe the problem with this {0} and we will look into it.

Flag this {1} Cancel

Advanced Search Reset

What

Where

How

or Cancel