Interview Question


Country: United States




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

As you can see, the *left* half has all the small elements while the *right* half has all the large elements. Thus, the problem is partition the array into two halves of equal sizes, left contains all the small elements ( w/o violating the order ) and right has the large ones.
Thus, for odd sized array the middle element would be the median.
For lack of editing capability, posting as comment.

- NoOne October 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

I cannot think of an in-place algorithm right now. but what we can do is:
1. Find the median in O(n) time using quickselect.
2. Using two pointers (one for the elements smaller than the median and one for the larger) copy the elements over to a buffer array preserving the relative order. Smaller elements will be placed in the buffer starting from index = 0 and larger elements will be placed in the array starting from n/2
We also need to handle both of the cases where the original array has even/odd number of elements.

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

package carrercup;
import java.util.*;

public class array1 {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int temp,temp1;
int[] arr=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int[] newarr=new int[n];
for(int r=0;r<n;r++)
{
newarr[r]=arr[r];
}
for(int j=0;j<n;j++)
{
for(int k=j;k<n;k++)
{
if(newarr[k]<newarr[j])
{
temp=newarr[k];
newarr[k]=newarr[j];
newarr[j]=temp;
}
}
}
temp1=newarr[n/2-1];
int count=0;
for(int t=0;t<n;t++)
{
if(arr[t]<=temp1)
{
temp=arr[t];

for(int u=t;u>count;u--)
{
arr[u]=arr[u-1];

}
System.out.println(temp1);
arr[count]=temp;
count++;


}
}
for(int o=0;o<n;o++)
{
System.out.print(arr[o]);
}
}
}

- yogesh.kumbhat October 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void PartitionArray(int* array, int cnt)
{
   int* pTmp = new int[cnt];
   std::copy(array, array + cnt, pTmp);
   std::nth_element(pTmp, pTmp + (cnt-1)/2, pTmp + cnt);
   int median = pTmp[(cnt-1)/2];
   
   for (int lIdx = 0, hIdx = (cnt + 1)/2, i = 0; i < cnt; i++)
      pTmp[(array[i] <= median) ? lIdx++ : hIdx++] = array[i];

   std::copy(pTmp, pTmp + cnt, array);
   delete[] pTmp;
}

- tjcbs2 October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Edited to remove extraneous copy

void PartitionArray(int* array, int cnt)
{
   int* pTmp = new int[cnt];
   std::copy(array, array + cnt, pTmp);
   std::nth_element(array, array + (cnt-1)/2, array + cnt);
   int median = array[(cnt-1)/2];
   
   for (int lIdx = 0, hIdx = (cnt + 1)/2, i = 0; i < cnt; i++)
      array[(pTmp[i] <= median) ? lIdx++ : hIdx++] = pTmp[i];

   delete[] pTmp;
}

- tjcbs2 October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// ZoomBA
def left_right( a ){
  len = #|a|
  odd = !(2 /? len)
  left_heap = fold( a , heap( len/2 + 1 ) ) -> { $.partial  += $.item  }

  #(l,r) = fold( a, [list(),list()] ) -> {
    if ( $.item < left_heap.max ){ $.partial.0 += $.item }
    if ( $.item > left_heap.max ){ $.partial.1 += $.item }
    if ( !odd && $.item == left_heap.max ) { $.partial.1 += $.item }
    $.partial
  }
  println ( l +  (odd ? left_heap.max : []) + r )
}

a = [ 6, 4, 5, 0, 2, 1, 11 , -1 ]
left_right(a)
a = [ 6, 4, 5, 0, 2, 1, 11 , -1, 9  ]
left_right(a)

- NoOne October 14, 2016 | 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