Microsoft Interview Question for Software Engineer / Developers






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

int A[5] = {1,3,8,9};
int B[9] = {2,4,5,6,7,0,0,0,0}; // last four as place holders for the merge

// start from the end of the larger array;
int idx = 8;
// we also need the indices of the largest elements in both arrays
int idx_a = 3, idx_b = 4;

while (idx_a >= 0) { // done when A has been traversed
    if (idx_b < 0 || A[idx_a] > B[idx_b]) { // if elements of b are exhausted
        B[idx] = A[idx_a];
        idx_a--;
    }
    else {
        B[idx] = B[idx_b];
        idx_b--;
    }
    idx--;
}

- airfang613 March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Neat !

- aimHigh April 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

very nice approach of moving from the back. I was just thinking how can it be done if we move from the first element.. I mean we will have additional overhead of shifting elements in the second array

- coder June 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Superb!

- Beowulf September 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Instead of get rid of the front spaces. We can avoid having the space by calculate the merged size in advance, say k. Then, begin your result index at k-1 th of the larger array. Doing will left no space in the front and guarantee no data element in the larger array being overridden by the data from the smaller array (in case when all element in the first array are greater than second array).

- Saimok March 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In this case we need to find out all duplicates.

Sum of (Array1 length + Array2 length)- duplicates => k-1.

We are increasing our iteration or loop here.

- N.M March 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question doesn't mention remove duplicates. Its just a merge. Why do you assume we need to remove duplicates?

- Anonymous June 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

copy the first array into the empty space on the second and then do a quick sort, would take nlogn and quicksort does not take extra space.

comments?

- jack March 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the above solution solves in O(n) so it would be better than your proposed solutions.

- NOBUG September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// assuming a is the larger array
void Merge(int[] a, int[] b, int m, int n)
{
int k = m + n - 1;
int curM = m - 1;
int curN = n - 1;

while (curM >= 0 && curN >= 0)
{
if (a[curM] >= b[curN])
{
a[k--] = a[curM--];
}
else if (a[curM] < b[curN])
{
a[k--] = b[curN--];
}
}

while (curN >= 0)
{
array[k--] = b[curN--];
}
}

- PL March 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

move all elements in 2nd array towards right and do merging

- Master Fuji April 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that the second array has extra empty space of the exact size as array 1, we can appraoch this problem in a smart way: start merging from Right to left side, i.e.

1. compare the last elements in both the arrays, whichever is greater, add it to the end of second array (which has extra space).
2. similarly keep moving towards smaller elements & add them from right to left in the second array.
3. Exit when all elements in array one are traversed

Using the above approach, no additional space is required. Secondly, when first array is completely traversed, automatically all the elements will be sorted in array 2.

- harleen June 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.dev.interview;

public class MergeTwoArrays {


	public static void main(String[] args){
		int arr[] = {1,2,54,76,100,0,0,0};
		int brr[] = {1,5,65};
		int l1 = arr.length -1;
		int l2 = brr.length -1;
		int x = 4;
		int y = 2;
		int k = 7;
		while(x>=0){
			//validation
			if(x<0 || y<0 || k<0){
				break;
			}
			
			if(arr[x] >  brr[y]){
				arr[k] = arr[x];
				k--;
				x--;
			}
			if(arr[x] <=  brr[y]){
				arr[k] = brr[y];
				y--;
				k--;
			}
			
		}
		
		//printing the output
		for (int i = 0; i < arr.length; i++) {
			System.out.println(" " + arr[i]);
		}
	}


}

- devkashyap046 August 22, 2015 | 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