Amazon Interview Question


Country: United States




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

use an extra 10-element sorted array. every time a new point is added, its distance is compared with the maximum of this array at one end and if larger, remove the largest and insert the new point. Could also use a max-heap.

- pdgetrf March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That would not work,, what if the element is some where between max and min, you cannot just enter it anywhere right?

Max- heap, is said of using priority heap, but complexity for it is nlogn which is very high compared to 0(1), so that too would not work.

- nr March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I said it wrong, if the new distance is smaller than the max of the 10-element array, which is at one end of the array since the array is kept sorted, simply throw away the max and insert the new distance in, much like in insertion sort. And since the array has up to 10 element, the insertion takes O(10) each time a new distance is inserted or O(1) if larger than the max. As to heap, we also only keep up to 10 elements in the max heap

- pdgetrf March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

convert the array to a heap, then the complexity is o (log(10))

- abcsea March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

o (log(10)) is O(1) :)

- barroca April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

10 nearest point to the centre, what centre?

- pdgetrf March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

By centre it means, point<0,0> and all the other points are anywhere in 4 quadrants

- nr March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

do we need to keep all points added or just to return the 10 nearest?

- pdgetrf March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have an array of points, will add from thr.....

But as i add points, i should be able to get the min distance point any time with 0(1).

- nr March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think a max-heap of 10 elements would work since at the time of adding new element T=T(10) = O(1)

- tma071000 March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yup max heep will certaily work here :)

- geeks March 31, 2012 | 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