Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Traverse through the list of points<lat, long>, calculate euclidean distance from given point and maintain a max heap of size 'k' where 'k' is the number of closest points we are expected to return. If, the heap_size is < 'k' just append new element to heap, else compare with max-heap root. if the new point is closer than farthest seen so far replace it with the new point seen and max-heapify.
If 'World' list contains all possible lat/long values then what is the point of finding nearest lat/long points. We can find matching lat/long points ryt.. And also what do u mean by 'nearest'??
I don't know if I understood it correctly, but I would suggest the following. First: What's the nearest location regarding two amazon stores? I would say, it's the way coming from one store to the other crossing this special location, with the minimum cost. So it's A1 -> ...-> P ->... -> A2 (A1 = 1st Amazon store 1, A2 = 2nd Amazon store, P = a location which has the feature, that cost(A1->P) + cost (P -> A2) is minimum). Whereby cost is the distance between to locations.
To do this, we use the ArayList "world" to build up a graph.
The elements of ArrayList "amazon" has to be a sub amount of ArrayList "world".
Since we don't know P, we should find the minimum way between A1 and A2 (e.g. by Dijkstra algorithm). If we cross one further position (which is an element of the ArrayList "world") we have the nearest place regarding two amazon stores. If we cross multiple Locations, those are equally nearest two those two amazon stores.
consider latitude and longitude as co-ordinates on 2D plane..
now it becomes simple..
A point-quad tree(refer wikipedia for "quadtree") will give the answer..
you can find the nearest shops in whatever direction(north-east(NE),NW,SE,SW) you want
A node of a point quadtree is similar to a node of a binary tree, with the major difference being that it has four pointers (one for each quadrant) instead of two ("left" and "right") as in an ordinary binary tree. Also a key is usually decomposed into two parts, referring to x and y coordinates. Therefore a node contains following information:
4 Pointers: quad[‘NW’], quad[‘NE’], quad[‘SW’], and quad[‘SE’]
point; which in turn contains:
key; usually expressed as x, y coordinates
value; for example a name
You can convert these two string into char arrays and sort them individually, run the loop till the size of the sort array and compare the starting elements of each array till the size of shortest array, if found any matching element do not add it to the new array else add it and convert that array into string again.
actually assume latitude,longitude be (x,y) now the question becomes finding nearest points(world) to a point(amazon) in two dimension this is simple , make two new array list of world one sorted according to x and another sorted according to y , take a point from amazon using binary search on these two list find four nearest points two according to x two according to y then find cartesian distance from all four points give the minimum .
I don't think it's right. say the store is at (0,0)
you have world points(1,10),(2,20),(10,1),(20,2),(3,3)
are you suggesting the four nearest points are the first four?
Should use kd-tree
- struggleyb February 03, 2013