Google Interview Question: Find the max value in an arra... | Glassdoor

## Interview Question

Intern Interview Los Angeles, CA

# Find the max value in an array. The array is "semi-sorted

". Here is an example: { 1 3 4 7 9 10 12 13 12 6 3 } As you can see, the array increases and then decreases at one point (13).

1

Simply walk the array to find the max in O(n) time. Compare the current max to the next element all the way until the end of the array.

Interview Candidate on Dec 9, 2013
2

O(logn) approach:

int max_value_in_semisorted_array(int* arr, int start, int end)
{
if (start == end) return arr[start];
int middle = (end + start) / 2;
if (arr[middle] &gt; arr[start])
return max_value_in_semisorted_array(arr, middle, end);
else
return max_value_in_semisorted_array(arr, start, middle);
}

Dídac Pérez on Dec 10, 2013
2

No, your O(logn)-approach doesn't work. Try this: 1 5 8 10 9 8 7 6 5 4

Sonya on Dec 20, 2013
2

The previous O(log n) approach doesn't handle arrays of odd length.

int findPeak(int * array, int start, int end)
{
if ((start == end) || ((start == (end - 1)) &amp;&amp; (array[start] &gt; array[end])))
return array[start];
if ((start == (end - 1)) &amp;&amp; (array[start] &gt; array[end]))
return array[end];
int middle = floor((start + end) / 2);
if (array[middle] &gt; array[start])
return (findPeak(array, middle, end));
else
return (findPeak(array, start, middle));
}

Remember to include math.h for the floor function.

Varsha on Dec 20, 2013
0

It seems, that it doesn't matter if the array is of odd or even length(btw, my example is not odd :)). This approach doesn't work when the max element is in the first half of your array and the middle element is greater than the first one. We cannot guarantee that if the middle element is greater than the first, our array increases monotonically from x[1] to x[middle] and we should search the max element in the range from middle to end.

Sonya on Dec 21, 2013
3

It is necessary to check two elements on each side of our middle element to find out where the array increases , where decreases, and where the peak is. And only then we can guarantee something about our max.

Sonya on Dec 21, 2013
1

The maximum is obviously just the inflection point in the list... some of the posted solutions here are absurdly complex for this.

nilkn on Jan 3, 2014
4

Brute force solution can be ofcourse O(N). But we can make it better:

mid = (start + end )/2

if mid &gt; mid + 1 &amp;&amp; mid &gt; mid - 1
return mid
else if mid mid -1
search mid -&gt; end
else if mid &gt; mid + 1 &amp;&amp; mid mid

Mo on Feb 14, 2014
0

@Mo - This won't be work for an array where the max element is hidden in the first / second half of the main array. Eg: {1,3,9,1,-2,6,2,2,5,-1}

Dee on Feb 14, 2014
2

In your test case, I see that the numbers increase, decrease, increases, decreases and so on. I thought the question says the list is semi sorted. So my program assumes that the numbers increase and then decrease. We need to find the peak. At least that is my understanding.

Mo on Feb 14, 2014
3

private static int recFindPeak(int[] a, int start, int end)
{
if(start == end)
return a[start];
int mid = (start+end)/2;
if((a[mid] &gt; a[mid+1])&amp;&amp;(a[mid] &gt; a[mid-1]))
return a[mid];
else if(a[mid] &gt; a[mid-1])
return recFindPeak(a, mid+1, end);
else
return recFindPeak(a, start, mid-1);
}

GReeky on Feb 27, 2014
0

My codes:

int findPeak(vector num) {
if (num.empty()) return 0;

int lower = 0;
int upper = num.size()-1;
while (lower num[mid+1])
return num[mid];
else if (num[mid-1] &lt; num[mid] &amp;&amp; num[mid] &lt; num[mid+1])
lower = mid+1;
else
upper = mid-1;
}
}

0

1. Walk from begin to mid comparing a[i] a[j] and stop decrementing j when this condition fails.
3. If a[i] &lt; a[j], then return a[j], else return a[i].

Anonymous on Jul 7, 2015
0

1) find the pivot of the highest number of the semi sorted list ,using binary search on the array.(logn)
2) From their do the linear serach for finding max ,also compare with last element of the semi sorted list.

Anonymous on Feb 21, 2016
0

public class KindsOfQuestion {
static int Max(int[] arr, int start, int end)
{
//int max=0;
if (start == end) return arr[start];
if((end-start)==1 &amp;&amp; arr[end]&gt;arr[start]) {
return arr[end];
}
if((end-start)==1 &amp;&amp; arr[end] arr[start])

if (arr[middle] &gt; arr[middle-1])
return Max(arr, middle, end);
else
return Max(arr, start, middle);

}

public static void main(String[] args) {
int[] arr=new int[]{1,5,8,10,9,8,7,6,5,4};//,TODO,Auto-generated,method,stub
System.out.println(&quot;Max is:&quot;+Max(arr,0,arr.length-1));
}
}

java code for case above on Sep 8, 2016
0

strange, some lines missing for above solution:
shouldn't be one line :
if((end-start)==1 &amp;&amp; arr[end] arr[start])

but:
if((end-start)==1 &amp;&amp; arr[end]&lt;=arr[start]) {
return arr[start];
}

Anonymous on Sep 8, 2016