Amazon Interview Question
SDETsCountry: United States
Interview Type: In-Person
public Integer[] find(Integer[] arr1, Integer[] arr2, Integer k) {
if ((null == arr1 && null == arr2) || k < 1) {
return null;
}
Integer[] result = new Integer[k];
// Identify the k lease numbers from arr1 and arr2
int index1 = 0, index2 = 0;
for (int count = 0; count < k; count++) {
// Identify the next lowest number and print it
// while accessing element in the array, Check #1. If array is not null #2. We have not consumed all the elements in the array
if (null != arr1 && index1 < arr1.length && arr1[index1] < arr2[index2]) {
result[count] = arr1[index1];
index1++;
} else if (null != arr2 && index2 < arr2.length) {
result[count] = arr2[index2];
index2++;
} else {
// Not enough number of elements in arr1 and arr2. k > arr1.length + arr2.length
break;
}
}
return result;
}
1. Create max heap of size k
2. iterate through first list
if no. of elements in heap is less than k
insert in heap
else
check if cur element is less than max value in heap
extract max from heap and insert cur value
3. repeat step 2 for second list
4. extract all elements of heap and print
// Time : O((m+n)*logk), Space : O(logk)
public static void findKSmallestElementsFromTwoLists(List<Integer> list1, List<Integer> list2, final int k){
final PriorityQueue<Integer> pq = new PriorityQueue<>(k, new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return i2-i1;
}
});
for(int i:list1){
if(pq.size() < k){
pq.offer(i);
}
else{
if(i < pq.peek()){
pq.poll();
pq.offer(i);
}
}
}
for(int i:list2){
if(pq.size() < k){
pq.offer(i);
}
else{
if(i < pq.peek()){
pq.poll();
pq.offer(i);
}
}
}
System.out.println(pq);
}
class COurList
{
public:
COurList()
{
iValue = 0;
pNext = 0;
}
int iValue;
COurList* pNext;
};
//.............................................................................................................
// Merge 2 sorted lists:
COurList* MergeSortedLists(COurList* pHead1, COurList* pHead2)
{
COurList *pCurrent1, *pCurrent2, *pCurrentRez, *pResultHead;
pCurrent1 = pHead1;
pCurrent2 = pHead2;
// what if we have only one valid pointer?
if ((!pHead1) && (pHead2))
return pHead2;
if ((!pHead2) && (pHead1))
return pHead1;
// first, set up the result header:
if (pCurrent1->iValue < pCurrent2->iValue)
{
pResultHead = pHead1;
pCurrent1 = pHead1->pNext;
pCurrentRez = pResultHead;
}
else
if (pCurrent1->iValue > pCurrent2->iValue)
{
pResultHead = pHead2;
pCurrent2 = pHead2->pNext;
pCurrentRez = pResultHead;
}
else
if (pCurrent1->iValue > pCurrent2->iValue)
{
pResultHead = pHead1;
pCurrent1 = pHead1->pNext;
pResultHead->pNext = pHead2;
pCurrentRez = pHead2;
pCurrent2 = pHead2->pNext;
}
// Now go through the list:
while (pCurrent1 && pCurrent2)
{
if (pCurrent1->iValue < pCurrent2->iValue)
{
pCurrentRez->pNext = pCurrent1;
pCurrentRez = pCurrent1;
pCurrent1 = pCurrent1->pNext;
}
else
if (pCurrent1->iValue > pCurrent2->iValue)
{
pCurrentRez->pNext = pCurrent2;
pCurrentRez = pCurrent2;
pCurrent2 = pCurrent2->pNext;
}
else
if (pCurrent1->iValue > pCurrent2->iValue)
{
pCurrentRez->pNext = pCurrent1;
pCurrentRez = pCurrent1;
pCurrentRez->pNext = pCurrent2;
pCurrentRez = pCurrent2;
pCurrent1 = pCurrent1->pNext;
pCurrent2 = pCurrent2->pNext;
}
}//while
// list remainder:
if (pCurrent1)
pCurrentRez->pNext = pCurrent1;
if (pCurrent2)
pCurrentRez->pNext = pCurrent2;
if (!pCurrent1 && !pCurrent2)
pCurrentRez->pNext = NULL;
return pResultHead;
}
//.............................................................................................................
void CutToFirstKelements(COurList* pHead, const int& k)
{
int i;
if (k <= 0) pHead=NULL;
for (i = 0; i < k - 1; ++i)
pHead = pHead->pNext;
pHead->pNext = NULL;
}
def least_of_k_elem():
l1 = [1, 13, 50, 184, 187, 239, 245, 262, 326, 391, 428]
l2 = [10, 74, 130, 151, 239, 336, 457, 494,517, 529, 544,]
k = int(input())
a = []
if (len(l1) and len(l2))< k:
print("enter the valid range of k")
else:
while k > 0:
if l1[0]<l2[0]:
a.append(l1[0])
l1.pop(0)
else:
a.append(l2[0])
l2.pop(0)
k -=1
print(a)
Here's my code in Java. If you see any optimization scope please do suggest. Thank!
- alam.adil12 September 22, 2016