## Amazon Interview Question for SDETs

Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
3
of 3 vote

Here's my code in Java. If you see any optimization scope please do suggest. Thank!

``````public void LeastKNum(int[] a, int[] b, int n){
int i=0,j=0,k=0;

while (k<n&&i<a.length&&j<b.length){
if (a[i]<b[j]){
System.out.println(a[i]);
i++;
}else{
System.out.println(b[j]);
j++;
}
k++;
}

while (k<n&&i<a.length){
System.out.println(a[i]);
i++;
k++;
}

while (k<n&&j<b.length){
System.out.println(b[j]);
j++;
k++;
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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;
}``````

Comment hidden because of low score. Click to expand.
0
of 2 vote

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);
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````class COurList
{
public:
COurList()
{
iValue = 0;
pNext = 0;
}

int iValue;
COurList* pNext;
};

//.............................................................................................................
// Merge 2 sorted lists:

{

// what if we have only one valid pointer?

// first, set up the result header:

if (pCurrent1->iValue < pCurrent2->iValue)
{
}
else
if (pCurrent1->iValue > pCurrent2->iValue)
{
}
else
if (pCurrent1->iValue > pCurrent2->iValue)
{
}

// 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;

}
//.............................................................................................................
void CutToFirstKelements(COurList* pHead, const int& k)
{
int i;

for (i = 0; i < k - 1; ++i)

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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)``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.