Interview Question


Country: United States




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

check middle element if i<a[i] go for first half middle, else right half middle. continue process till we find the answer.

- master May 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It would be fun, if the problem would have been like this

A[1] <= A[2] <= ..... <= A[n]

Then it would have become more complex because of duplicates.

- Nitin Gupta May 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene.yarovoi: In worst case it will be O(n), but in average case it will be O(log n).

- Nitin Gupta May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

binary search will do.
pseudocode:

{
	binarysearch(i, begin, end){
		middle = (begin+end)/2
		if(A[middle]==i) return middle
		else if (A[middle]< i) call binarysearch(i,middle+1,end)
		else  call binarysearch(i, begin,middle-1)
		}

- Anonymous May 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think , u can't use i as an function parameter. our logic should look for all possible index

- om May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes binary search is the optimal solution for this which would compute this in O(logn) time

- ram rs May 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) If A[n]<n then there is no such element exists as A[1] < A[2] <...< A[n]..
2)if A[1]>1 then there is no such element exists as A[1] < A[2] <...< A[n]..
Above two conditions are also important..

- kunapareddy.sunil May 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, that part is missing
pseudocode is modified adding those conditions:

binarysearch(i, begin, end){
		middle = (begin+end)/2
		if(A[middle]==i) return middle
		else if (A[middle]< i) call binarysearch(i,middle+1,end)
		else if  (A[middle]> i) call binarysearch(i, begin,middle-1)
		else "no such element exists"
		}

- Anonymous May 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Locates an index i such that A[i] = i 

array - input sorted list
from - from index
to  - to index

returns  an i such that A[i] = i; otherwise -1.
*/

int bin_search_index_match(int* array, int from, int to)
{
  /* if the first elt is bigger than its index then we stop searching 
     because our input list is sorted and increasing. No index can catch the content */

  if(array[from] > from)
    return -1;

  int mid = from + (to - from)/2;

  if(array[mid] == mid)
    return mid;

  if(array[mid] > mid)
    return bin_search_index_match(array, from, mid-1);
  else
    return bin_search_index_match(array, mid+1, to);
}

- x May 12, 2013 | 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