Amazon Interview Question for Software Engineer / Developers






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

this is a C++ concept - that states point to member - n->0 translates to n to member functions(m).

use a selection algorthm with :

function select(list[1..n], k)
for i from 1 to k
minIndex = i
minValue = list[i]
for j from i+1 to n
if list[j] < minValue
minIndex = j
minValue = list[j]
swap list[i] and list[minIndex]
return list[k]

- Anonymous February 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Heapify the array in O(m) as a max heap and then remove n elements from the heap in O(nlogm).

Overall complexity: O(m + nlogm) which will be almost linear as n->0.

- ~amv February 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take an array N of size 'n'. For each element in array, compare with elements in N and make sure that N always has the max three elements.

Finally find the smallest number in N. This will be O(m) coz n is much lesser than m.

- Mahesh February 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int N=5

For[i=0;i<N-1;i++]
{
For[j=1,K=0;j<N-1,K<3;j++]
{
If( A[i]<A[j] && ( (A[j] < B[K]) || K=0 ) )
{
B[K]=A[j]
}
}
K++;
}

Console.writeline(B[K])

- Tester February 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

run partial quicksort. at each level check if m is less than the no of elements on the left partion. if so execute quick sort on the left else on the right with m = m - no of elements on right. if final pivot index == m return value in m. This is linear on an average case.

- Anonymous February 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is THE ONLY correct answer

- Saurabh February 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is nothing but a Selection algorithm. This solution seems to be the best, considering no extra array space or heap space.

- aquila.25 March 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question clearly states you cannot sort.

- Anonymous January 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

But the question says "without sorting".
How about making a bin search tree out of elements and then searching it?

- cirus February 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesnt sort, it just reduces the search space.
when the median is in place, it is done. It doesnt complete sorting

- Anonymous March 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about a binary min-heap(BMH), similar to ~amv's suggestion.
But no need to heapify the whole array of m elements.
Only keep the n largest sorted.

FOR (list[i=0...m]) { # O(m)
IF ( i <= n )
THEN
insertToBMH(list[i], BMH) # O(log n)
ELSE
min = minValue(BMH) # O(1), just get the root
IF ( list[i] > min )
swap(list[i], BMH-root) # O(1)
heapfiyDown(BMH) # O(log n), bubble-down list[i] from root
END-IF
END-IF
END-FOR

nth-largest = getRoot(BMH) # O(1)

Overall, O(m * 2 * log n)) = O(m log n)

- Anonymous February 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is discussed in cormen

have a look at section 9.3
worst case O(n)

- nithin March 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given: m elements in the array, n<<m
To Find: n th smallest element

Sol: allocate an array of n elements
as you traverse the list of m elements, compare each of the element with n elements in the array
you'll end up with n smallest of m
Complexity: O(log n)*O(m) => O(m) since n<<m

- Neil March 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is called a Selection Algorithm. Please check for the explanation in wikipedia.

- Anonymous March 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i agree its a selection algorithm. perform selection algorithm till m-n steps.

- Anonymous March 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for small n << m select algorithm is useful

- uts April 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int quickSelect(int[] a,int p,int r, int i)
	{
		if(p==r) return p;
		int q=randomizedPartition(a, p, r);
		int k=q-p+1;
		if(i==k)
			return q;
		else if(i<k)
			return quickSelect(a, p, q-1, i);
		else
			return quickSelect(a, q+1, r, i-k);
	}

- MaYank April 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in array if space complexity doesnt matter the u can do it by making the binarey seach tree of n numbers and finding the kth largest number by just doint the Inorder Traversal :)
but if it also mattrers and u can not rearrange the array it is sure to have that solution will take O(n2) at worse case
if it is right to rearrange the order of elemnts then u can only able tio solve this problem in O(n) solution :)

- geeks July 30, 2011 | 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