Expedia Interview Question for Software Engineer / Developers






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

compare successive elements of array.. if a[i]<=a[i+1] proceed
else /*a[i] > a[i+1] */
stop here u have find the point from where the array should actually start. now return the rotation..i think u can compute that

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

Code in C.

int find_rotation(int arr[],int size)
{
int index = 1;
while(index++<size)
{
if(arr[index - 1] > arr[index])
break;
}
retun (size - index);
}

- Sanjay June 03, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this takes care of the duplicate entries also

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

awesome!

- choti January 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the above algthm takes O(n) which is not good.
it didn't take advantage that the original array is sorted. The best sln will take O(lgN)

int find_rotation(int arr[],int size)
{
int index = 1;
if (size < 2) return 0;
if(arr[0] < arr[size-1]) return 0;

if ((arr[0] < arr[size/2])
{
return find_rotation(&arr[size/2],size/2)
}
else
{
return size/2 + find_rotation(arr,size/2)
}
}

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

what about the memory concern when you use recursion?

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

Here is the code without recursion. I tried for couple of inputs and it seems to work. Plz let me know if someone finds a bug in it..

#include<stdio.h>
void main()
{
	int i, a[100],n,start,mid,end,count=0;
	printf("Enter the total no. of elements in the array \n");
	scanf("%d", &n);
	printf("Enter the elements in the rotated array \n");
	for(i=0;i<n;i++)
		scanf("%d", &a[i]);
	start = 0;
	end = n-1;
	while(start != end)
	{
		mid = start+(end-start+1)/2;
		if(a[start] < a[mid])
			start = mid;
		else
		{
			count = mid;
			end = mid-1;
		}
	}
	printf("The rotation count is %d \n", (count)?n-count:0);
}

- gauravk.18 February 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

When duplicate is allowed. The above algorithm does not work.
Say a[] = { 1, 2, 2, 2, 2, 2, 2, 2, 3}
a2[] = { 2, 2, 2, 2, 2, 2, 3, 1, 2}

- kulang November 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Take size and last element(3) from array a.
loop through array a2 untill last occurence of last element of array a (don't count).
From there count the no. of elements untill you reach the size.

- prashanth. September 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If all the elements are equal then there cannot be a unique solution. Otherwise we can simply use binary search kind of technique to solve the problem in O(logn). The problem can be rephrased as find the least index in the given array such that arr[index]<arr[0].
The answer will be n-index. index is 0 based!. I suppose it is very easy to solve the new problem I phrased.

- python.c.madhav August 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int findRotation(int[] a) {
        if ( a != null) {
            int startPosition = 0;
            int length = startPosition = a.length;
            for (int i=0; i<length; i++) {
                if ((i+1) != length && a[i+1] < a[i]) {
                    startPosition = i+1;
                    break;
                }
            }
            return length - startPosition;
        }

        return 0;
    }

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

Store the beginning of each array in a hash. For the array given iterate that array and increment number to where you find it.

Best case O(1). Worst case O(n)

- Albert February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;

#define SIZE 9

int rotation(int a[], int low, int high)
{
int mid;

mid = (low+high)/2;

if (a[low] <= a[mid])
{
return rotation(a, mid, high);
}
else
{
return low;
}
}

int main()
{
int a[] = {2, 2, 2, 2, 2, 2, 3, 1, 2};

int result = rotation(a, 0, SIZE - 1);

cout<<result<<endl;

system("PAUSE");
return 0;
}

Worst Case Complexity O(logn)

- Amit Priyadarshi May 09, 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