xyz Interview Question for Applications Developers


Team: 5
Country: India
Interview Type: Written Test




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

There is a sorting algorithm called external merge sort that can help you with this. Essentially in the beginning you sort the two smaller arrays using something like quicksort, so you have 1 = {2,5,8,9} and 2= {1,3,4,6,7}. Then you check the first element of both arrays, and put the smaller in the third array: min(1[0], 2[0]), so now you have 3 = {1}. Then you "advance" to the second element in the array that you used, and now compare the second element of that array and the first element of the other array, so you check min(1[0], 2[1]). So now array 3 looks like 3 = {1,2}.

- cemkadioglu91 April 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

break the two arrays into individual elements and then use merge sort to get combined sorted array.

- aojha4 April 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 0, j = 0;
            while (i + j < array3.Length)
                if (j == array2.Length || ((i < array1.Length) && (array1[i] < array2[j])))
                    array3[i + j] = array1[i++];
                else
                    array3[i + j] = array2[j++];

Is this the correct way to merge 2 arrays...

- navishsahu April 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class MergeArray {
    private void heapSort(int[] arr) {
        ...
    }

    public int[] mergeIntoSortedArray(int[] arr1, int[] arr2) {
        sorting(arr1);
        sorting(arr2);
        int[] out = new int[arr1.length + arr2.length];
        int j = 0;
        int k = 0;
        int i = 0;
        while (j < arr1.length && k < arr2.length) {
            if (arr1[j] < arr2[k]) {
                out[i++] = arr1[j++];
            } else {
                out[i++] = arr2[k++];
            }
        }

        while (j < arr1.length) {
            out[i++] = arr1[j++];
        }

        while (k < arr2.length) {
            out[i++] = arr2[k++];
        }
        return out;
    }
}

public class MergeTwoArrayIntoSortedArray {
    public static void main(String[] args) {
        int[] arr1 = new int[] {2,5,8,9};
        int[] arr2 = new int[] {6,3,4,7,1};
        MergeArray mMergeArray = new MergeArray();
        System.out.println("MERGED SORTED ARRAY: "
                + Arrays.toString(mMergeArray.mergeIntoSortedArray(arr1, arr2)));
    }

}

- Scorpion King April 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MergeSort {

public static void main(String[] args) {
int[] array1 = { 2, 5, 8, 9 };
int[] array2 = { 6, 3, 4, 7, 1 };
int[] array3 = new int[array1.length + array2.length];
array3 = getSortedArray(array1, array2, array3);
for (int i = 0; i < array3.length; i++) {
System.out.print(array3[i] + " ");
}
}

private static int[] getSortedArray(int[] array1, int[] array2, int[] array3) {
array2 = sortArray(array2);
array1 = sortArray(array1);
int a1 = 0, a2 = 0, a3 = 0;
while (a1 < array1.length && a2 < array2.length) {
if (array1[a1] < array2[a2]) {
array3[a3] = array1[a1];
a1++;
a3++;
} else {
array3[a3] = array2[a2];
a2++;
a3++;
}

}
while (a1 < array1.length) {
array3[a3] = array1[a1];
a1++;
a3++;
}
while (a2 < array2.length) {
array3[a3] = array2[a2];
a2++;
a3++;
}
return array3;
}

private static int[] sortArray(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i] < array[j]) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
return array;
}

}

- Rishabh Tiwari April 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MergeSort {

	public static void main(String[] args) {
		int[] array1 = { 2, 5, 8, 9 };
		int[] array2 = { 6, 3, 4, 7, 1 };
		int[] array3 = new int[array1.length + array2.length];
		array3 = getSortedArray(array1, array2, array3);
		for (int i = 0; i < array3.length; i++) {
			System.out.print(array3[i] + " ");
		}
	}

	private static int[] getSortedArray(int[] array1, int[] array2, int[] array3) {
		array2 = sortArray(array2);
		array1 = sortArray(array1);
		int a1 = 0, a2 = 0, a3 = 0;
		while (a1 < array1.length && a2 < array2.length) {
			if (array1[a1] < array2[a2]) {
				array3[a3] = array1[a1];
				a1++;
				a3++;
			} else {
				array3[a3] = array2[a2];
				a2++;
				a3++;
			}

		}
		while (a1 < array1.length) {
			array3[a3] = array1[a1];
			a1++;
			a3++;
		}
		while (a2 < array2.length) {
			array3[a3] = array2[a2];
			a2++;
			a3++;
		}
		return array3;
	}

	private static int[] sortArray(int[] array) {
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array.length; j++) {
				if (array[i] < array[j]) {
					int temp = array[j];
					array[j] = array[i];
					array[i] = temp;
				}
			}
		}
		return array;
	}

}

- Rishabh Tiwari April 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String []args)
	{
		int a[] = {2,5,8,9};
		int b[] = {6,3,4,7,1};
		
		int c[] = new int[a.length+b.length];
		
		//copy both a,b to c
		int i=0;
		for(int j=0;j<a.length;j++)
			c[i++] = a[j];

		for(int j=0;j<b.length;j++)
			c[i++] = b[j];

		//now merge sort
		Arrays.sort(c);
		System.out.println(Arrays.toString(c));
	}

- saumya.wipro April 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

That is the main challenge here...
I do not have to use any in built methods as well as i need to maintain complexity...

- navishsahu April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

That is the main challenge here...
I do not have to use any in built methods as well as i need to maintain complexity...

- navishsahu April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
Is below code is ok?? Don't know about complexity. Please comment for its complexity and if anything wrong in code. {{{ public class MergTwoArrays { public static void main(String[] args) { int[] arr1 = {0,2,5,8,9}; int[] arr2 = {6,3,4,7,1,8,12}; arr1 = sortArray(arr1); for(int x: arr1){ System.out.print(x+","); } System.out.println("\n"); arr2 = sortArray(arr2); for(int x1: arr2){ System.out.print(x1+","); } System.out.println("\n"); int[] mergedAndSortedArray = Merg(arr1,arr2); for(int a : mergedAndSortedArray){ System.out.print(a+","); } } public static int[] Merg(int[] arr1, int[] arr2){ int i=0,ls = 0; int le = arr1.length-1; int j=0,rs = 0; int re = arr2.length-1; int[] finalArray = new int[arr1.length+arr2.length]; int k=0; while(i <=le && j<=re){ if(arr1[i]<arr2[j]){ finalArray[k] = arr1[i]; i++; }else{ finalArray[k] = arr2[j]; j++; } k++; } while(i<=le){ finalArray[k] = arr1[i]; i++; } while(j<=re){ finalArray[k] = arr2[j]; j++; } return finalArray; } public static int[] sortArray(int[] arr){ for(int k=1;k<arr.length;k++){ int temp = arr[k]; int j = k-1; while(j>=0 && arr[j]>temp){ arr[j+1] = arr[j]; j--; } arr[j+1] = temp; } return arr; } } }} - Shahid Akhtar April 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MergTwoArrays {

public static void main(String[] args) {

int[] arr1 = {0,2,5,8,9};

int[] arr2 = {6,3,4,7,1,8,12};

arr1 = sortArray(arr1);

for(int x: arr1){
System.out.print(x+",");
}
System.out.println("\n");
arr2 = sortArray(arr2);
for(int x1: arr2){
System.out.print(x1+",");
}
System.out.println("\n");
int[] mergedAndSortedArray = Merg(arr1,arr2);
for(int a : mergedAndSortedArray){
System.out.print(a+",");
}

}

public static int[] Merg(int[] arr1, int[] arr2){

int i=0,ls = 0;
int le = arr1.length-1;
int j=0,rs = 0;
int re = arr2.length-1;
int[] finalArray = new int[arr1.length+arr2.length];
int k=0;
while(i <=le && j<=re){

if(arr1[i]<arr2[j]){
finalArray[k] = arr1[i];
i++;
}else{
finalArray[k] = arr2[j];
j++;
}
k++;
}
while(i<=le){
finalArray[k] = arr1[i];
i++;
}
while(j<=re){
finalArray[k] = arr2[j];
j++;
}

return finalArray;

}

public static int[] sortArray(int[] arr){

for(int k=1;k<arr.length;k++){
int temp = arr[k];
int j = k-1;
while(j>=0 && arr[j]>temp){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
return arr;
}

}

- Shahid Akhtar April 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MergTwoArrays {

public static void main(String[] args) {

int[] arr1 = {0,2,5,8,9};

int[] arr2 = {6,3,4,7,1,8,12};

arr1 = sortArray(arr1);

for(int x: arr1){

System.out.print(x+",");

}

System.out.println("\n");

arr2 = sortArray(arr2);

for(int x1: arr2){

System.out.print(x1+",");

}

System.out.println("\n");

int[] mergedAndSortedArray = Merg(arr1,arr2);

for(int a : mergedAndSortedArray){

System.out.print(a+",");

}

}

public static int[] Merg(int[] arr1, int[] arr2){

int i=0,ls = 0;

int le = arr1.length-1;

int j=0,rs = 0;

int re = arr2.length-1;

int[] finalArray = new int[arr1.length+arr2.length];

int k=0;

while(i <=le && j<=re){

if(arr1[i]<arr2[j]){

finalArray[k] = arr1[i];

i++;

}else{

finalArray[k] = arr2[j];

j++;

}

k++;

}

while(i<=le){

finalArray[k] = arr1[i];

i++;

}

while(j<=re){

finalArray[k] = arr2[j];

j++;

}

return finalArray;

}

public static int[] sortArray(int[] arr){

for(int k=1;k<arr.length;k++){

int temp = arr[k];

int j = k-1;

while(j>=0 && arr[j]>temp){

arr[j+1] = arr[j];

j--;

}

arr[j+1] = temp;

}

return arr;

}

}

- Shahid Akhtar April 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MergTwoArrays {

	public static void main(String[] args) {
	
		int[] arr1 = {0,2,5,8,9};
		int[] arr2 = {6,3,4,7,1,8,12};
		arr1 = sortArray(arr1);
		for(int x: arr1){
			System.out.print(x+",");
		}
		System.out.println("\n");
		arr2 = sortArray(arr2);
		for(int x1: arr2){
			System.out.print(x1+",");
		}
		System.out.println("\n");
		int[] mergedAndSortedArray = Merg(arr1,arr2);
		for(int a : mergedAndSortedArray){
			System.out.print(a+",");
		}

	}
	
	public static int[] Merg(int[] arr1, int[] arr2){
		
		int i=0,ls = 0;
		int le = arr1.length-1;
		int j=0,rs = 0;
		int re = arr2.length-1;
		int[] finalArray = new int[arr1.length+arr2.length];
		int k=0;
		while(i <=le && j<=re){
			
			if(arr1[i]<arr2[j]){
				finalArray[k] = arr1[i];
				i++;
			}else{
				finalArray[k] = arr2[j];
				j++;
			}
			k++;
		}
		while(i<=le){
			finalArray[k] = arr1[i];
			i++;
		}
		while(j<=re){
			finalArray[k] = arr2[j];
			j++;
		}
		
		return finalArray;
		
	}
	
	public static int[] sortArray(int[] arr){
		
		for(int k=1;k<arr.length;k++){
			int temp = arr[k];
			int j = k-1;
			while(j>=0 && arr[j]>temp){
				arr[j+1] = arr[j];
				j--;
			}
			arr[j+1] = temp;
		}
		return arr;
	}

- Shahid Akhtar April 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;

public class MergeTwoArra {



public static void main(String[] args) {
// TODO Auto-generated method stub

int a[] = {2,5,8,9};
int b[] = {6,3,4,7,1};
int c[] = new int[a.length + b.length];
int i =0;

HashMap<Integer,Integer> int_map = new HashMap<Integer,Integer>();

int map_size = a.length;
map_size += b.length;
int value=0;

for(i=0;i<a.length;i++)
{
value = int_map.getOrDefault(a[i], 0);
int_map.put(a[i], ++value);
value = 0;
}


for(i=0;i<b.length;i++)
{
value = int_map.getOrDefault(b[i], 0);
int_map.put(b[i], ++value);
value=0;
}

int count= 0;

for(i=0;i<=map_size;i++)
{
value = int_map.getOrDefault(i, 0);
if (value > 0)
{
for(int j=0;j<value;j++)
{
c[count++] = i;

}
}

}


}

}

- Anonymous April 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void arrayMerging() {
int a[]={1,3,4,5,6};
int b[]={7,8,9,10,11};
int c[]=new int[a.length+b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
Arrays.sort(c);
try{
for(int i=0;i<c.length;i++){
System.out.print(c[i]+" ");
}
}catch (Exception e) {
e.printStackTrace();
}

}

- Arun Kumar R April 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void arrayMerging() {
		int a[]={1,3,4,5,6};
		int b[]={7,8,9,10,11};
		int c[]=new int[a.length+b.length];
		System.arraycopy(a, 0, c, 0, a.length);
		System.arraycopy(b, 0, c, a.length, b.length);
		Arrays.sort(c);
		try{
	    for(int i=0;i<c.length;i++){
	    	System.out.print(c[i]+" ");
	    }
		}catch (Exception e) {
			e.printStackTrace();
		}

}

- Arun Kumar R April 27, 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