Amazon Interview Question for Software Engineer / Developers






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

If K number does not have to be consecutive, just sort them and added up the last ones.. But I think it has to be consecutive, else there is no point. Well then raghu.slp will need to be more clear before just posting. =)

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

looked for both the cases, didn't like the idea of sorting a a file with huge number of integers, that itself was another question.

- raghu.slp May 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

find top k elements from the file using mean heap. Sum them up

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

Max-heap approach would result in the following operations :-
1 Build max-heap = O(n)
2 Get top k numbers = Max-heapify applied O(k) times = O(klogn).
So in all time = O(n+klogn)
In worst case, when k = O(n), time = O(nlogn).
Better way :-
1 Get kth largest number, by randomized select - O(n)
2 Partition around this number to get numbers larger than the kth largest number - O(n).
Total time = O(n) , k doesnt appear.

- Sriram May 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you have calculated the complexities wrongly.

1. We assume max heap has size k. So the heapify operation will have complexity of O(log k). This has to be done n times. So total time complexity O(nlog k).
2. Then sum up all the elements in the heap - O(k)

So O(nlog k+ O(k))

- abhimanipal May 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A O(nlogk) solution is possible:

1. Built a balanced binary tree with the first k elements of the tree and store the least element in a separate variable.- O(klogk)
2. If new element greater than the least element, remove least element from tree and reorder the tree - O(logk)
3. Above step done for all n values - O(nlogk)
4.Sum up all the elements of the tree after the entire list is traversed - 0(k)

Total Complexity - O(nlogk+klogk+k)

- Robben May 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

With k being small the solution is O(nlogk)

- Robben May 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

same can be done using minheap:
Make minHeap using the first k integers. O(K)
Then for all the subsequent values compare them with the head. if head is small then the value then replace head and do proclateDown. Do the same for all the values.
At the end add all the elements of the array(heap).

- Hemant May 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(K)+ O(nlogK) = O(nlogK)

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

Yup, maintain a min heap of k elements. O(nlogK)

- neo May 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if you dont want to use a max heap to save the O(k) space...you could use the quick select algorithm(finding Kth largest element algo) from CLRS..O(kn) time.

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

if you dont want to use a max heap to save the O(k) space...you could use the quick select algorithm(finding Kth largest element algo) from CLRS..O(kn) time.

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

with huge number of integers, the select algo is inefficient since you would make multiple passes through the file. much better to maintain a minheap in memory(if sufficient memory is available) and make a single pass through the large file.

- cubic May 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't we use linear select to find Kth rank..

- MaYanK May 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[]{32,3,1,213,42,3,13,3};
int p = 0;
int r = a.length-1;
int k=4;
quickSort(a,p,r);
quickSelect(a,p,r,k);
for(in i=k;i<=r;i++)
System.out.println(a[i]);
}

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

private static int randomizedPartition(int[] a, int p, int r) {
// TODO Auto-generated method stub
int n = (int)((r-p+1) * Math.random()) ;
n=n+p;
int x = a[n];
a[n]=a[r];
a[r]=x;
return partition(a,p,r);
}

private static int partition(int[] a, int p, int r) {
// TODO Auto-generated method stub
int x = a[r];
int i = p-1;
for(int j=p;j<r;j++)
{
if(a[j]<=x)
{
i=i+1;
int temp = a[j];
a[j]=a[i];
a[i]=temp;
}
}
int temp = a[r];
a[r]=a[i+1];
a[i+1]=temp;
return i+1;
}
}}}

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

public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = new int[]{32,3,1,213,42,3,13,3};
		int p = 0;
		int r = a.length-1;
		int i=k; //here is k input
		quickSort(a,p,r);
		quickSelect(a,p,r,i);
                int sum=0;
		for(i=k;i<=r;i++)
			sum=sum+a[i];
	}
	
	private static void quickSort(int[] a,int p ,int r)
	{
		if(p>r)
			return;
		int q = randomizedPartition(a,p,r);
		quickSort(a,p,q-1);
		quickSort(a,q+1,r);
		
	}
	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);
	}

	private static int randomizedPartition(int[] a, int p, int r) {
		// TODO Auto-generated method stub
		int n = (int)((r-p+1) * Math.random())  ;
		n=n+p;
		int x = a[n];
		a[n]=a[r];
		a[r]=x;
		return partition(a,p,r);
	}

	private static int partition(int[] a, int p, int r) {
		// TODO Auto-generated method stub
		int x  = a[r];
		int i  = p-1;
		for(int j=p;j<r;j++)
		{
			if(a[j]<=x)
			{
				i=i+1;
				int temp = a[j];
				a[j]=a[i];
				a[i]=temp;
			}	
		}
		int temp = a[r];
		a[r]=a[i+1];
		a[i+1]=temp;
		return i+1;
	}

- Mayank May 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We Don't need Quick Sort remove it .. I had code so forgot while copy paste:-)

- MaYank May 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use a queue of size k
enqueue if scanner.nextInt() > q.front

at the end sum the elements in the queue

time comlexity = O(n)

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

i think the complexity is O(nlgk)

- razor June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What will happen, when first enqueued element is the largest one.
This wont help anymore.
min heap is the best sol.

- Raghav June 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find (N-K)th largest element using meadian finding algorithms - O(n)
Now partition into elements with this element as pivot - O(n)
Sum up the largest K elements = O(K) = O(n)

Total is O(n)

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

I think Sriram solution is best.

1 Get kth largest number, by randomized select - O(n)
2 Partition around this number to get numbers larger than the kth largest number - O(n).
Total time = O(n) , k doesnt appear.

- Srikant Aggarwal November 12, 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