Yahoo Interview Question for Software Engineer / Developers






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

do you mean find the two indices?

- Anonymous September 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

start with finding the max and min of the array and check if min is at beginning or max is at the end .if either of them occurs to be true then accordingly change the start and end of array.recurse it until the max and min within the array boundaries(start and end) dont happen to be at the either extremities.at that point sort the array in length start and end.

- saumils1987 September 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very good solution. Bravo!

- Anonymous February 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is O(n^2). One optimization could be to keep a global Min[] and Max[] array, where Min[i] and Max[i] are the current min and max seen so far.
When we recurse(start,end) we should make sure A[end] == Max[end] and Min[start] <= A[start]

- Anonymous February 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The below algo assumes the size of array is even. With few modifications to the below code, it can be supported when array of size is odd. N is the size of array.

public ret[] returnIndices(int[] a, int size) {
 int n1=0, n2 = size - 1;
 for(;n1<n2;n1++,n2--) {
   if(a[n1] > a[n2]) break;
   if(a[n1] < a[n1-1])  { n1--; break; }
   if(a[n2] > a[n2+1])  { n2++; break; }
 }
ret[0] = n1;
ret[1] = n2;
return ret;
}

I took the below example & it works fine : a = {1,2,6,4,5,7,8,9}

- Krish February 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The below algo assumes the size of array is even. With few modifications to the below code, it can be supported when array of size is odd. N is the size of array.

public ret[] returnIndices(int[] a, int size) {
 int n1=0, n2 = size - 1;
 for(;n1<n2;n1++,n2--) {
   if(a[n1] > a[n2]) break;
   if(a[n1] < a[n1-1])  { n1--; break; }
   if(a[n2] > a[n2+1])  { n2++; break; }
 }
ret[0] = n1;
ret[1] = n2;
return ret;
}

It works fine for this array : a = {1,2,6,4,5,7,8,9}

- Krish February 12, 2012 | 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