Google Interview Question

Get the kth largest number from two sorted arrays

Interview Answers

Anonymous

Jul 2, 2012

// return kth biggest element from 2 sorted arrays public static int kthElement (int[] a1, int[] a2, int k){ int i1 = a1.length-1; int i2 = a2.length-1; for (int i =1; i a2[i2]) { i1--; } else { i2--; } } if (a1[i1] > a2[i2]) return a1[i1]; else return a2[i2]; }

4

Anonymous

Jul 10, 2012

the solutions 1) start looking for the arrays a1 and a2, as they are both sorted. you can move ahead when needed. This solution is O(k). the same as implementation on the above 2 implementation. 2) The interviewer is probably looking to find the following answer. lets say for choosing the k element you choose l element from array a1 and k-l element from a2. the condition to be satisfied here is a1[l]

1

Anonymous

Jun 23, 2012

let they be a[m]&b[n] int i,j,p=0 for(p=1;p

2