Interview Question


Country: United States




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

The code will take a binary search approach with few checks in between. Complexity = O(logn)

int FindPivotIndex(vector<int> arr)
{
	int size = arr.size();

	int first = 0, last = size - 1;
	while (first <= last)
	{
		if (arr[first] <= arr[last])
			return first;
		int mid = (first + last) / 2;

		if (arr[mid] < arr[first] && arr[mid] <= arr[last])
		{
			if (arr[mid - 1] > arr[mid])
				return mid;
			// pivot in first half
			last = mid - 1;
		}
		else if (arr[mid] >= arr[first] && arr[mid] > arr[last])
		{
			//pivot in second half
			first = mid + 1;
		}
	}	

	return -1;
}

- Brandy May 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

def find_pivot(array):
    left = 0;
    right = len(array) - 1
    cur = (right + left) / 2

    while left < cur < right:
        cur = (right + left) / 2

        if array[cur] <  array[right]:
            left = cur
        elif array[left] < array[cur]:
            right = cur


    return cur

- glebstepanov1992 May 23, 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