Interview Questions

We have two unique sorted arrays........

Microsoft Interview Questions and Answers


(Continued from previous question...)

117. We have two unique sorted arrays........

Question:
We have two unique sorted arrays. Need to find common elements from these arrays.


maybe an answer:
you have two sorted array of unique elemets
a1 = {1, 3, 5, 6}
a2 = {3, 4, 6}

take two indexes ia1 & ia2 pointing to a1[0] & a2[0] respectively

compare both elements pointing by index
if(a1[ia1] == a2[ia2])
{
/* found common element a1[0] */
}
else
if(a1[ia1] < a2[ia2])
{
ia1++; /* increment to compare with next element */
}
else
{
ia2++ /* increment index of second array*/
}
}

perform above steps until any one array is exhausted

(Continued on next question...)

Other Interview Questions