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

- alam.adil12 September 22, 2016 | Flag Reply
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;
	}

- AR September 16, 2016 | Flag Reply
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);
	}

- Raj September 16, 2016 | Flag Reply
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:

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

- sergey.a.kabanov September 17, 2016 | Flag Reply
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)

- mohsum October 27, 2018 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More