Interview Questions

Given 2 sorted integer arrays, find the intersecting element in them.

Microsoft Interview Questions and Answers


(Continued from previous question...)

95. Given 2 sorted integer arrays, find the intersecting element in them.

Question:
Given 2 sorted integer arrays, find the intersecting element in them.


maybe an answer:


/* The class name doesn't have to be Main, as long as the class is not public. */
class check {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//int[] arr = new int[5];
int arr[]={1,2,4,6,7,8,10,100,600,1000};
int arr1[]={1,2,3,4,5,6,8,9,10,100,200,300,400,700,1000};
int i=0,j=0,k=0;
while(i < arr.length)
{
for(;j < arr1.length;)
{
if(arr[i] == arr1[j])
{
System.out.println(arr[i]);
i++; j++;
break;
}
else if(arr[i] < arr1[j])
{
i++;
}
else if(arr[i] > arr1[j])
{
j++;
break;
}
}
}
}
}



maybe an answer2:


let the two arrays be array1[]
array2[]
for(i=0; j=0; i<array1.length(); j if(array1[i]== array2[j]) {
print("found the element %d", array1[i]);
} else if (array1[i] < array2[j]) i++;
else j++;
}

(Continued on next question...)

Other Interview Questions