Microsoft Interview Question
Software Engineer in TestsCountry: United States
Interview Type: In-Person
Array1 = M+N space and M elements
Array2 = N element
Shift all elements of Array1 to right such that Array[M+N]=Array[M]..... Array[N+i]=Array[i]... Array[N+1]=Array[1]
Now point Array1 at N+1 and Array2 at 1
compare Array1[N+1] and Array2[1]
whichever is greater put it at Array[1]
and increment correspondingly and so on....merge
Assuming arr1 has space for m+n elements, but contains only m elements and arr2 for n elements.
- Anonymous January 17, 2012Take two pointers pointing on end of both arrays data, call it rdIndex1 and rdIndex2, and one pointer pointing on absolute end of arr1, calling it wrIndex
Now whichever of arr1[rdIndex1] arr2[rdIndex2] is larger write it at arr1[wrIndex], and decrement corresponding rdIndex and wrIndex.
continue till both of rdIndex become zero.