Amazon Interview Question for Software Engineer / Developers






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

the complexity should be O(n), please check introduction to algorithm, finding the Kth number in the array, its complexity is O(n), but anything on the left of that number is also larger than K.

- anonymous September 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think u understood the question wrongly. the array is unsorted and u have to find the K largest elements in an array of size n

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

even in an unsorted array kth element can be found in O(n),then travel the array to find all numbers greater than that

- kaka September 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How do you break ties ? Say k=4 from the array 123333445, SELECT returns 3, obviously cannot just pick all the 3s.
So far heap seems best with O(klgn).

- David March 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, max(O(n), O(klgn))

- David March 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

David - its easy to fix that. After finding the (n-k+1)th element(since we are looking for k largest elements), make a pass and print all numbers GREATER than the this element. If we printed less than k numbers then we ran into your case. So we make another pass and print the remaining elments which are same as the n-k+1 th element.

- NK June 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes we can find the kth element in O(n) time using the select algorithm, though the worst case complexity is O(n^2) for that algo, but i think the algo is sufficient at the time of interviews.

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

People, look at the question properly. The k LARGEST *ELEMENTS* are supposed to be returned. So, the answer has to be k elements and not just the kth largest.

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

yeah... its k LARGEST *ELEMENTS* ..see my previous solution

- kaka September 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yeah... its k LARGEST *ELEMENTS* ..see my previous solution

- kaka September 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Okay,
Sort the array in O(nlogn) in descending order and return the first K elements.

This should be O(n log n ) complexity.

- Anonymous September 29, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use quickselect(arr, 0, n-1) and inside go thro only one side where k belongs

- mail2vcp@gmail.com September 30, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Instead of min heap why dont you max heap.And get the k maximum elements in O(klogn).

- Erik March 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(k logn)?

Isn't this O(n) in the worst case? You have to look at each element at least once... don't you?

There are selection algorithms to select the kth largest in O(n) time. Use that, then do a pass of the array and just pick elements >= the kth largest you just found.

O(n), not matter what your k is.

To use a max-heap:
First heapify the array: O(n) time.
Then get the max k times : O(k log n).

So depending on k, it could potentially be worse than the selection of kth largest, O(n) time solution.

For small k, max-heap is probably better, but probably depends on the implementation etc.

- T March 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we can so this in O(n)
a) First find the kth biggest no. which can be done in logn (variant of Quicksort)
b) Scan the array for all number greater than or equal to kth biggest no. which is O(n)

- joy May 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i dont understand why people are unnecessarily writing craps even the first couple of responses nails the problem. Have a max heap of size K, and traverse the full list each time putting the element in the heap and maintaining the max-heap property, by the end you've all the k-largest elements in the heap and we're done. Time: O(n) as we have to traverse the whole list to fill the maxheap.

- abe dhakkan September 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah i understand what u wanna say . But here the time complexity will be O(n log k).
You will traverse the whole list once i.e. O(n) and will put every element in the max heap of size k which is O(log k).
So the overall becomes O(n log k).

- gulusworld1989 October 17, 2010 | Flag


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