Amazon Interview Question for Software Engineer / Developers






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

Not sure if this is best soln:
1. Store x co-ordinates in a BST (Say X BST)
2. For y co-ordinates , Design a Hash table where key will be corresponding x value
. Here many y can have same x, so store all such y in a BST, and slot for x key in Hash table will point to the root of this BST
Using these Data Structures, All x will be in one BST and All y will be in corresponding x key BSTs in Hash Table.

Searching a given (x,y):
1st look of x in X BST (log n), if not found, return -1, else look for y in Hash table, in x key BST(log n).
Seach Time: log n

Finding Closest Point for a given (x,y):
1. Look for x JUST smaller then given x, look for y JUST smaller than given y, calculate the distance (say d1)
2. Look for x JUST smaller then given x, look for y JUST greater than given y, calculate the distance (say d2)
3. Look for x JUST greater then given x, look for y JUST smaller than given y, calculate the distance (say d3)
4. Look for x JUST greater then given x, look for y JUST greater than given y, calculate the distance (say d4)
5. point of MIN(d1,d2,d3,d4) is the closest point.
Time: log n

This design looks scalable, Any number of new points can be added anytime.

- Anonymous February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A little addition:

Finding Closest Point for a given (x,y):
1. Look for x JUST smaller then given x, look for y JUST smaller than given y, calculate the distance (say d1)
2. Look for x JUST smaller then given x, look for y JUST greater than given y, calculate the distance (say d2)
3. Look for x JUST greater then given x, look for y JUST smaller than given y, calculate the distance (say d3)
4. Look for x JUST greater then given x, look for y JUST greater than given y, calculate the distance (say d4)
5. Look for same x, look for y JUST greater than given y, calculate the distance (say d5)
6. Look for same x, look for y JUST smaller than given y, calculate the distance (say d6)
7. Look for same y, look for x JUST greater than given x, calculate the distance (say d7)
8. Look for same y, look for x JUST smaller than given x, calculate the distance (say d8)
9. point of MIN(d1,d2,d3,d4,d5,d6,d7,d8) is the closest point.

- Anurag Singh February 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am afraid it is not that simple. Consider the following points (1,3), (2,100), (3,3). Now the point closest to (3,3) is (1,3) which your logic doesn't consider at all.

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

you can use a HASH Table for it.. like a array of pointers(structure pointer)and then from every index(use it as x coordinate) and store the y coordinate in the array[index]..and like wise for all those numbers whose x coordinate are same..just cr8 a link list from that index which will point to all points of same x coordinate..

struct *number[].

store only the y coordinate let say point (0,1) . cr8 a node .store 1 and store the address of the node in number[0]..and so on go on storing all those number having coordinates after the node which stored 1..as a single link list..

in this way we can store all points..

and then for searching first check X coordinate and then go to that number[x coordinate) to move to the starting node of that point and search for y coordinate in that link list...( u can get nearest point to it in this easily..)

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

How about k-d tree
Building a k-d tree: O(n log 2 n)
Insertion; O(logn)
Searching closest point: O(sqrt(n))

- GekkoGordan February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. Look like k-d tree is the right answer.

- Anurag Singh February 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I spent 30 min to understand k-d tree but I couldn't.

- siva.sai.2020 February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Look at Animation of NN searching with a KD Tree in 2D on wiki
that might help visualize. thanks

- chennavarri February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

please someone put some gud tutorial like on K-d tree ...

- sg .. February 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if k == 1, a kd-tree is nothing but a binary search tree (BST). A node has a single key (x) and for each node all the keys in the left-subtree of the node are less-than this key and all the keys in the right-subtree are greater-than or equal to this key.
if k == 2, a kd-tree is almost the same as BST except for a minor change. Note that each node now has two keys (x, y). Instead of using only one key we will try to use both of them. Let 'l' be the level/height/depth of a node 'n' in the tree. Assume that the root is at level 0. If ('l' mod 2 == 0), all the (x-keys) in the left sub-tree are less-than the x-key of 'n' and all the (x-keys) in the right-subtree are greater-than are equal to the x-key of 'n'. On the other hand if ('l' mod 2 == 1) the rule will be applied to thr y-keys. It is just a modified-BST with the comparison rule applied to the k-keys depending on the level of the node. This may sound confusing, but it helps to visualize the structure as follows. For the root (rx, ry), all the points in the left sub-tree are to the left of the vertical line X = rx and those in the right sub-tree are to its right.
Let the root of the left sub-tree be (lx, ly). For this node all the points in the left sub-tree are to the bottom of the horizontal line (Y = ly) and those in the right sub-tree are to its top. The same horizontal line logic goes for the root of the right sub-tree (rx, ry).
NOTE: Even though I said horizontal line (Y = ly), it is infact a line-segment (Y = ly && (X < rx)). See the article on wikipedia for a visual example.
The above logic can be extended to k-dimensions easily.

- Anonymous February 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks

- rahul February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

For calculating shortest distance we can use formula (squareroot of (x-x1)^2+(y-y1)^2). here x,y are the given points and x1,y1 can be iterated from hash map

- Neha February 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous, nice explanation

- siva.sai.2020 March 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

2. Divide and Conquer Solution.
See cormen chapter 33 Computational Geometry.
Complexity O (n log n).

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

you have to store the (x,y) in a structure and maintain the array of this structure for storing all the points...
a) We have to sort all the element based on x cooridinate
b) then we have to use divide and conquer to get the smallest distance..
For more info refer cormen or mark alen weiss

- swathi July 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Manhattan distance from origin as value in BST for all points. To search a point compute its Manhattan distance from origin and search in the tree to find the close values. o(log n)

- raja December 15, 2017 | 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