Yahoo Interview Question for Software Engineer / Developers






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

void rearrange(int A[],int begin,int end)
{
if(begin+1==end)
return;
if(begin<end){
int mid=(begin+end)/2;
int r1=(begin+mid)/2;
int r2=(mid+1+end)/2;
int d=mid-begin;
int i,j;
if(d%2!=0){
j=mid+1;
for(i=r1+1;i<=mid;i++)
swap(&A[i],&A[j++]);
rearrange(A,begin,mid);
rearrange(A,mid+1,end);
}
else{
j=mid+1;
for(i=r1;i<=mid;i++)
swap(&A[i],&A[j++]);
swap(&A[mid],&A[mid+1]);
rearrange(A,begin,mid-1);
rearrange(A,mid+2,end);
}
}
}

- Manish October 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This in an O(n log n) solution.

Explanation :
We divide the array in four sections:[X,Y|A,B]
It is easy to see that with swaps we can modify it to the form [X,A|Y,B].
Now do recursion to solve [X|A] and [Y|B] separately,essentially using divide and conquer.
-------------------------------------

For example:
Original Array: [X,Y|A,B] = { 1,2,3,4,5,6,7,8,a,b,c,d,e,f,g,h }
where X = { 1,2,3,4 }, Y = { 5,6,7,8 }, A = { a,b,c,d }, B = {e,f,g,h}
Now, swap sections Y and A
New array : [X,A|Y,B] = { 1,2,3,4,a,b,c,d,5,6,7,8,e,f,g,h }

We now have divided original problem in to two similar sub problems by doing n/2 swaps.
We can use recursion to solve the sub problems now.

-------------------------------------

Complexity Analysis :
T(n) = 2T(n/2) + O(n)
=> T(n) = O(n log n)

- Mayank Jaiswal February 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are also other ways of doing this in O(n log n), for instance one can define the appropriate comp() function that compares the integers and characters and then just sort the array with respect to that function.

- Afshin January 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Mayank : If your using a temporary array, then it makes no sense. Because using temporary array, we can get it in O(n) itself. Please explain if I am wrong :)

- Kushal March 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here's my idea...

char[] organize( char[] string, int n )
    int distance = n;
    for ( int i = 0; distance > 0; i += 2, distance-- ) {
        char ci = string[i+distance+1];
        for ( int j = i + distance; j > i; j--) {
            string[j+1] = string[j];
        }
        string[i+1] = ci;
    }
}

- Igor S. October 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

rearrange( A[] , begin, end )
{
 if(begin<end){
        mid = (begin+end)/2
        r1 = (begin+mid)/2
        r2 = (mid+end)/2
        swap elements of r1-mid with mid-r2
        rearrange( A , begin, mid)
        rearrange( A , mid+1, end)

}
}

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

The element at the ith position in the final array will be at position
(i%2)*N + i/2 in the original array

Above logic can further extended for 'k' ;

k--> nos of groups of elements that compose given the given array

- koolkeshaw July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void swap(int arr[],int i,int j){
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
static void rollBack(int arr[],int right,int left){
for(int i=right;i>left;i--)
arr[i] = arr[i-1];
}
static void reArrange(int array[]){
int index = array.length/2;
for(int i=1;i<array.length-1;i+=2){
int tmp = array[i];
swap(array,i,index);
rollBack(array, index++, i);
array[i+1] = tmp;
}
}

- Ashkan October 19, 2014 | 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