## Microsoft Interview Question Software Engineer / Developers

• 0

Given two sorted arrays where the size of second array is large enough to hold the first array, write code to merge them (in sorted order). Write test cases

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

Approach:
1. Set the iterator in both the array
2. Compare the item and do just increment, or increment with FirstArray right shift by one position.
3. Augment Second array to FirstArray if it still remains after first iterator finishes.

``````int [] MergeSortedArray(int FirstArray[], int elementCountInFirstArray, int SecondArray[], int sizeofSecondArray){
int i=0,j=0;
while(i<elementCountInFirstArray){
if(FirstArray[i] >= SecondArray[j]){
if(FirstArray[i] == SecondArray[j]){
i++;
}
int k= i;
while(k<elementCountInFirstArray){
FirstArray[k+1] = FirstArray[k];
k++;
}
FirstArray[i]=SecondArray[j];
j++;
elementCountInFirstArray++;
}
i++;
}
while(j<sizeofSecondArray){
FirstArray[i++] = SecondArray[j++];
}
return FirstArray;
}``````

Thanks
Ankush

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

Algorithm:-

Let the two arrays be a[n], b[m+n+c]//m elements & c >=0
Let i be the iterator for a[]
Let j be the iterator for b[]
Let k be the position that needs to be filled

i = index for last element of a[] = n-1
j = index for last element of b[] = m-1
k = m+n-1;

``````while(i>=0 && j>=0)
{
if(a[i]>b[j])
b[k--] = a[i--]
else
b[k--] = b[j--]
}
while(i>=0)
{
b[k--] = a[i--];
}``````

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

int[] MergeSortedArray(int firstArray[],int secondArrady[], int firstSize,int secondSize){
int k = firstSize + secondSize-1;
while(firstSize>=0 && secondSize>=0)
{
if(a[firstSize]>b[secondSize])
{
b[k--] = a[firstSize--];
}
else
{
b[k--] = b[secondSize--];
}
}
while(i>0)
{
b[k--] = a[firstSize--];
}
}

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

nice algo! one more thing to consider: one array in ascending order while the other in descending orde.

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

Copy all the items into the large array and perform a sort again. O(n log n) if you compare each item and push into the large array from the small array it will require moving items in the large array which is boring and useless.

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

let i & j point to element in arrays A & B
and k points to position to be filled in B(assuming B is larger one)

``````k = m+n-1
i = m-1
j = n-1``````

instead of merging from the beginning, start merging from the end(in decreasing order).

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

### 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.