Interview Question


Country: United States




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

i=j=k=0;
while (i <= a.length && j <= b.length)
{
if a[i] < b[j];
{
final[k] = a[i];
i++;
}
else
{
final[k] = b[j];
j++;
}
k++
}

while (i <= a.length)
{
final[k]=a[i];
i++
k++
}

while (j< = b.length)
{
final[k]= b[j];
j++;
k++;
}

- Ketki January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

same as merge sort last step which is combine.

- siren0413 January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I found the solution in the book titled "Elements of Programming Interviews".

The solution is a special case of "Merging sorted files" problem in that book. You can Google and find their solutions in C++ at Google Code.

- Anonymous January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A general solution that can be used for any number(m) of sorted arrays of variable length: here m=4;

#include<stdio.h>
#include<stdlib.h>
int smallest(int *p[],int n, int *size, int *counter)
{
  int i;int min=100;
  
  for(i=0;i<n;i++)
  {
  	if(counter[i]!=size[i])
    if(*(p[i])<min)
    {
      min=*p[i];
    }
  }
  for(i=0;i<n;i++)
  {
  	if(counter[i]!=size[i])
    if(min==*p[i])
    {
	  p[i]++;
      counter[i]++;
      break;// Remove break if you want to remove duplicates
    }
  }
  return min;
}

int main()
{
  
int a0[4]={2,5,7,9};
int a1[4]={3,6,8,14};
int a2[5]={4,7,9,11,22}; 
int a3[5]={7,8,9,22,40};
int m=4;//number of arrays
int *p[4]={a0,a1,a2,a3};//array of pointer storing each array pointer
int size[4]={sizeof(a0)/sizeof(a0[0]),sizeof(a1)/sizeof(a1[0]),sizeof(a2)/sizeof(a2[0]),sizeof(a3)/sizeof(a3[0])};
  int i,k=0;
  int counter[m];
  int B[100];
  int total_size=0;
  for(i=0;i<m;i++)
  {
     counter[i]=0;
     total_size+=size[i];
  }
  //printf("%d ",total_size);
  while(k<total_size)
  {
    int s=smallest(p,m,size,counter);
    //if(s==100)   Add this if you are removing duplicates;
      // break;
    B[k++]=s;
  }
  for(i=0;i<k;i++)
    printf("%d\n",B[i]);

}

- Shashi January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For this one it might be faster to use sort and parallel pointers?

- SL January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Perform merge sort

- Abhi January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CombineArrays {

public static int[] combineArrays(int[] arr1, int[] arr2){
int idx1 = 0;
int idx2 = 0;
int idx3 = 0;
int[] arr3 = new int[arr1.length+arr2.length];
while (idx1 < arr1.length || idx2 <arr2.length){
if (idx1 < arr1.length && idx2 < arr2.length && arr1[idx1] < arr2[idx2]){
arr3[idx3++] = arr1[idx1++];
}
else if (idx2 < arr2.length){
arr3[idx3++] = arr2[idx2++];
}
else if (idx1 < arr1.length){
arr3[idx3++] = arr1[idx1++];
}
}
return arr3;
}

public static void main(String[] args){
int[] arr1 = {1,2,4,7,9};
int[] arr2 ={2,8};
int[] res = combineArrays(arr1,arr2);
for (int i=0; i<res.length; ++i){
System.out.println(res[i]);
}
}

}

- Liron - O(n+m) January 25, 2013 | 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