转自 :http://www.careercup.com/question?id=9691840
Amazon Interview Question Software Engineer / Developers
-
0of 0 votes
Given an infinite size array with only 0s and 1s and sorted. find the transition point where 0s end or 1s start (written test question. coding)
0
of
0vote
The idea is to find upper bound for 1 by exponentially increasing the index in power of 2 and then finding the transition index by exponentially decreasing the index.
Similar to binary search.
Complexity 2*log(N+1) where N is the transition index.
A is the input array
1. start=0, end=1, mid=0
2. if((A[start]==0)&&(A[end]==1)) return start;
3. while(A[end] != 1)
start=end;
end=end<<;1;
4. while(A[start+1] != 1)
mid=(start+end)/2;
if(A[mid]==0)
start=mid;
else
end=mid;
5. return start;
This can be done by modified binary search.....
- n20084753 on July 21, 2011 | FlagReply1.At first check the 2^i the index i=0,1,2,....
2.If '1' occurs at some point of index [example say 32 i.e. 2^5]
3.Apply binary search between 2^4 and 2^5 [in infinite array the problem is we ill not have the info of high to apply binary search....]
correct me if i am wrong ,thanks in advance..