Amazon Interview Question for Software Engineer / Developers






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

Above solutions are too trivial, a better solution would be
compute x^2 + y^2 (no need sqrt!) and transform into another array. O(n)
Next,
build a Max Heap (Q) out of the first k elements in the array. ---> O(k)
For all elements from k+1..n , if a[i] < Q.min() ,
then Q.replace(Q.min() , a[i])
Q.downheap()
The above loop will take a total time of O(nlogk) in the worst case.

After the loop terminates, the contents of the heap is the k closest elements to the origin.
The total running time of this algorithm = O(n + nlogk)

If you use the trivial sorting method, it will take O(nlogn) . If n is very large, and if k is small, then you can see how inefficient sorting is.

- Brave Heart November 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

small correction u have to compare Q.max() to a[i]
and replace Q.max() with a[i] if a[i] < Q.max()

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

Yes, thanks :)

- Brave Heart November 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

agnel's solution is good. O(nlogk) will be output sensitive.

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

@agnel~ computing x^2 + y^2 for all x&y will take O(n2) and not O(n)

- Anonymous December 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi!
Each x and y are grouped together as a single point. There are n such points. Hence O(n)

- Brave Heart December 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you should insert the element into the heap without comparing a[i] to Q.max(). Why are you comparing a[i] with Q.max()?

- Anonymous December 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hi people correct me if m wrong but why wud u do all this...after u get an array of n elements just use select for (n-k)th position O(n) time ( finding ith position element ) and then all elements above it will be ur answer

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

compute euclidean distance and sort by distance

- Synonymous November 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

d = sqrt(x*x + y*y)
get min(d) by comparing after each computation d = ..

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

The trick is that you dont have to compute all distances :-) Try to come up with a partitioning algorithm that computes distance only for K Points and not more :-)

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

I am sorry, you need to compute the distances but not sort them :P You can use a linear implementation of an algorithm that searches K minimum elements in an Array.

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

Hi, I don't think there is a linear algorithm to search for k min/max elements in an array that is not sorted.
It will be > O(n) no matter which algorithm you use.

- Brave Heart November 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

abhijeet is right.
use the select algorithm (en.wikipedia.org/wiki/Selection_algorithm)to find the kth minimum element ,this takes o(n) time.after finding the kth minimum element iterate over the array to find all the elements less than the kth minimum element found.so overall complexity is o(n).

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

This is efficient in terms of run-time, but requires additional memory of O(n) (for selection algo). agnel.joel solution is memory optimal as it requires a heap of size O(k). if k is not close to O(n), heap approach is memory optimal.

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

boss , how can you find a kth minimum element in o(n) time ... it will take o(n*k) ....

- rhtdm48 December 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

guyz there are really smart people browsing this site,no wonder many problems dont have their solutions posted.

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

and, you are one of those 'smart' people.

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

and, you are one of those 'smart' people.

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

bhaiyon jhagda mat karo yaron!!!!
@dingdong wat ur saying man select jus takes o(1) space,u dont need auxillary array to compute the kth smallest element.

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

dbdgfdgfdgfdgf

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

1. Create a Max Heap with the first k points using the distance factor..
2. For each remaining point, insert it in the Max Heap and then delete the max value from the heap.
3. The heap now contains the min k points.
4. This algorithm is O(n log m), where m is the number of points we are looking for.

- Newbie January 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. calculate the distance of each point from the origin
2. Pick the first point P1, let its distance be R1.
3. Find no. of points which are inside the circle of radius R1, lets call them NR.
Three cases
a. if NR > K R1 = R1/2
b. if NR == K we are done
c. else R1 = R1*2
repeat step 3.

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

1. Calculate the distances of each point once and store the distances in an array. O(N).
2. Now find the kth smallest distance in the array using Selection Rank algorithm. O(k).
3. Iterate over the N points again from the beginning and get all points whose distance is less than or equal to k and write them onto your k-size array. O(N).
considering N>>>k then total time complexity O(N).

- superman February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Check this out O(2n) using a quicksort mod. nice..
google: "Find Kth minimum element in a unsorted array ashish yadav" (donno why I can't put the link here!!)

- agniva July 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use K-D trees if you have the all the points in the plain at the beginning.
And also this approach can be extended to points in k dimensions.

below link might help, how to create K-D trees:
ms-amazon.blogspot.in/2013/06/implementation-of-k-dtrees.html

- varun October 24, 2013 | 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