Interview Question for Software Engineer / Developers


Team: Java
Country: India
Interview Type: In-Person




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

Suppose you have the following inputs:
a[]
b[]
c[]
d[] // destination array of size 3*m
m // the size of each small array

1. Merge the two of the arrays and start placing in the destination array at index "m",
thus leaving space for "m" number of elements at the beginning of the array.
2. Now start merging the third array with the larger array in the destination array
located at position "m"

void merge(int a[], int b[], int c[], int d[], int m)
{
    int i = 0, j = 0, didx;
    
    // Merge the first two arrays into the largest starting at index "m"   
    didx = m; // destination index   
    while (i < m && j < m){
        if (a[i] <= b[j]) {
            d[didx] = a[i];
            ++i;
        }
        else {
            d[didx] = b[j];
            ++j;
        }    
        ++didx;
    }

    if (i < m) {
        while(i < m){
            d[didx] = a[i];
            ++didx;
            ++i;
        }
    }
    else {
        while(j < m){
            d[didx] = b[j];
            ++didx;
            ++j;
        }
    }

    // Now merge the last array into the destination
    // [0] .... [m] ...... [3*m - 1]
    i = 0;
    j = m;
    didx = 0;
    while (i < m && j < 3 * m) {
        if (c[i] <= d[j])
            d[didx++] = c[i++];
        else
            d[didx++] = d[j++];
    }

    // If there is a tail in first array, then copy it to the destination
    // otherwise the rest of the elements are in their proper positions
    while (i < m)
        d[didx++] = c[i++];
}

- ashot madatyan July 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice.

- Murali Mohan July 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes a very good solution. Thumbs up..

- vgeek July 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

but to mention here please mention that m is not the length of the array but it is m-1 as in that case the array will take the addresses as the elements as you are doing while(i<m) but actually i is only to be run till m-1 that is:
while(i<m-1)
but just to mention you can only call the function with m-1 rather than making changes to the whole function..

- vgeek July 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

.I did not understand 'Merge the two of the arrays and start placing in the destination array at index "m", thus leaving space for "m" number of elements at the beginning of the array' . Your solution is as good as merging arrays a[ ],b[ ] into d[ ] and then merging d[] with c[ ] . Please let me know if I am wrong

- Shiva July 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@Shiva
Yes, you got it right - merge a[0] and b[0] into d[m], then merge c[0] and d[m] into d[0].

- ashot madatyan July 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Use 4 pointers from the ends of the 3 sorted arrays as well as the larger array.
1. Use 3 way comparison to find out the maximum element of the 3 sorted arrays and place it at the end of the larger array. Decrement the pointer of the larger array and of the sorted array where the maximum was found.
2. Repeat step 1 until one of the array is exhausted.
3. Start doing 2-way comparison and placing the maximum value in the larger array
4. Repeat step 3 until only one array remains
5. Copy the contents of the remaining sorted array into the larger array.

- Murali Mohan July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nyc... I also thought of the same solution. I have a doubt though.
Will this solution or the one suggested by - ashot madatyan will give less number of comparisons?

- Joey July 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the number of comparisons in both the cases is the same. In order for an element to be placed at it's correct position in the larger array 3 comparisons are required in both the cases.

- Murali Mohan July 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the best sol.
By merging 2 then 3rd may requires more no. of element replacements.

- PKT July 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int[] threeWayMerge(int[] a, int[] b, int[] c) {
	if (a == null || b == null || c == null) {
		return null;
	}

	int[] res = new int[a.length + b.length + c.length];
	System.arraycopy(a, 0, res, 0, a.length);
	twoWayMerge(res, a.length, b);
	twoWayMerge(res, a.length + b.length, c);
	return res;
}

public void twoWayMerge(int[] large, int count, int[] small) {
	int p1 = count - 1;
	int p2 = small.length - 1;
	int last = count + small.length - 1;
	while (p1 >= 0 && p2 >= 0) {
		large[last--] = large[p1] > small[p2] ? large[p1--] : small[p2--];
	}
	if (p2 >= 0) {
		System.arraycopy(small, 0, large, 0, p2 + 1);
	}
}

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

Initialise the next element in each array as infinite. Then start merging the three arrays by finding the minimum number from particular indecis of three arrays and incrementing the index of array with minimum number

- 3139a1m July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ashot madatyan :.I did not understand 'Merge the two of the arrays and start placing in the destination array at index "m", thus leaving space for "m" number of elements at the beginning of the array' . Your solution is as good as merging arrays a[ ],b[ ] into d[ ] and then merging d[] with c[ ] . Please let me know if I am wrong

- Shiva July 03, 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