Interview Question
Country: India
Interview Type: Written Test
public int[] merge(int a1[], int a2[]) {
int[] a3 = new int[a1.length + a2.length];
int k = 0;
int i = 0, j = 0;
k = 0;
while (i < a1.length && j < a2.length) {
if (a1[i] < a2[j]) {
a3[k] = a1[i];
i++;
} else {
a3[k] = a2[j];
j++;
}
k++;
}
while (i < a1.length) {
a3[k] = a1[i];
k++;
i++;
}
while (j < a2.length) {
a3[k] = a2[j];
k++;
j++;
}
return a3;
}
Assuming that interviewer wanted your own implementation of compare function.
- Pradeep Kashyap June 29, 2014Write your own sort function, with function to compare two strings.. Sort the first name and then last name.. Now print last name and then last name