Microsoft Interview Question

One array has size n and n elements . second array has size n+m and m elements. both the arrays are sorted. we need to get n+m elements in second array in sorted order. Linear time algorithm was expected

Interview Answer

Anonymous

Jan 13, 2011

#!/usr/bin/python def merge_arrays(a,b,n,m): out_idx = n+m-1 b_idx = m-1 a_idx = n-1 for out_idx in range(n+m-1,-1,-1): if b_idx >= 0 and a[a_idx] = 0 b[out_idx ] = a[a_idx] a_idx -=1 print b return b a = [0,1,2] b = [21,22,23,24,-1,-1,-1] print merge_arrays(a,b,len(a),len(b)-len(a))

1