Expedia Interview Question for Software Engineer / Developers






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

The amount "i" rotated, can be found in o(n) time. After finding "i", standard binary search can be applied with an additional mapping to the index. Index k has to mapped to (k-i+n)%n before indexing the array

- Anonymous December 13, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pseodocode:array index starts from 0,1,...
------------------------------------------------------------
p=given number to find
N=size of array
k=index of smallest element //amount of rotation in O(N) time
l=left index=k
r=right index = (k-1+N)%N
l1=transform(l)=(l-k+N)%N
r1=transform(r)=(r-k+N)%N

while(l1<=r1)
{
mid1=(l1+r1)/2
mid=inverseTransform(mid1)=(mid+k)%N
if(p== [mid]) break
//p occurs at index mid. [mid] indicates value at index=mid
if(p> [mid])
{
l1=mid1+1
}
else
{
r1=mid1-1
}
}

//code to check if p doesn't exist in array
-----------------------------------------------------
Note: variables l,r,mid indicates the indices of the given array
and variable l1,r1,mid1 are indices if the array would have not
been rotated,i.e. the lowest index (zero) contains smallest element

- kg January 15, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i is give apply bin_search in the two halves separately and get the ans.

- aashka's hub May 17, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Don't u think the runtime will be O(n+logn) if the amount of rotation is unknown?

- kg January 15, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You should be able to take advantage of the fact that the array is sorted and I think you can use an idea similar to binary search to find rotation i in O(log(n)) time.

- RT January 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Binary search with some extra conditions will be the way to find the element in log n time.

- satheeshramdass February 07, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

compare the value to be found with the middle element and also the first and the last element of the subarray(entire array - the very first time) to decide in which half to search

- annie March 04, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Divide and conquer. Compare first, middle, last to decide which half contains the max. This allows to find the shift i in O(lgN). Then use the shift and do binary search. Also O(lgN). Overall, it is O(lgN).

- Anonymous February 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n= total size
Find(a,low,high,i,val)
{
if a[i]>a[(i+1)%n]
Left Rotation with i=n-i;
else if a[i]<a[i+1]
Right Rotation with i=i;
else if a[i]=a[i+1]
return Find(a,low,high,i-1);
mid= ((low+high)/2 + i)%n;
low = (low+i)%n;
high = (high+i)%n;
if(val < a[mid])
return ((low==(mid-1+n)%n) ? -1: Find(low, (mid-1+n)%n));
else if(val > a[mid])
return (((mid+1)%n==high)?-1:Find((mid+1)%n,high));
else if(val == a[mid])
return mid;
}

Please comment. Didnt tested thorougly

- YetAnotherCoder October 02, 2008 | 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