Location: Bay area
Experience: 6 years
Webex Interview
Very simple question. Find x in an array which is first increasing and then decreasing. Assume there are no duplicates.
I couldn't solve the edge cases in findPivot function in time so reject is a self-prophecy. However, later I came up with this approach. This question is a little different from finding x in a pivoted array because in the latter version you know the subarray you are going to use to search for x. Also, in this case a common binary search function won't work as one of the pivoted array will be in increasing order and the other one will be in the decreasing order.
__author__ = 'deepika'
class Solution:
def findPivot(self, arr, left, right, n):
while left <= right:
if left == right:
return left
mid = right - (right - left)/2
if mid - 1 >= 0 and mid + 1 < n and arr[mid - 1 ] < arr[mid] > arr[mid + 1]:
return mid
if (mid - 1 >= 0 and mid + 1 < n and arr[mid] > arr[mid + 1] and arr[mid] < arr[mid - 1]):
right = mid - 1
else:
left = mid + 1
return n-1 #Strictly increasing array
def binarySearch(self, arr, left, right, element, greaterThanFunction):
while left <= right:
mid = right - (right - left)/2
if arr[mid] == element:
return mid
if greaterThanFunction(arr[mid], element):
right = mid - 1
else:
left = mid + 1
return -1
def findElement(self, arr, x):
pivot = self.findPivot(arr, 0, len(arr)-1 , len(arr))
index = self.binarySearch(arr, 0, pivot, x, lambda x, y : x > y )
if index == -1:
index = self.binarySearch(arr, pivot+1, len(arr) - 1, x, lambda x, y : y > x )
print("Found x: ", x, " at pivot: ", pivot)
return index
s=Solution()
s.findElement([1, 2, 3, 4, 5], 5)
s.findElement([4, 5, 3, 2, 1], 5)
s.findElement([5, 4, 3, 2, 1], 2)
s.findElement([-10, -9, -8, -7, -6, -5, 0, 7, 5, 4, 3, 2], -10)
s.findElement([-10, -9, -8, -7, -6, -5, 0, 7, 5, 4, 3, 2], -8)
s.findElement([-10, -9, -8, -7, -6, -5, 0, 7, 5, 4, 3, 2], 2)
s.findElement([-10, -8, 0, 7, 5, 4, 3, 2], -10)
s.findElement([-10, -8, 0, 7, 5, 4, 3, 2], -8)
s.findElement([-10, -8, 0, 7, 5, 4, 3, 2], 2)
Comments (6)


