Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

int doesRectOverlap(rect ra, rect rb){
    /* For your reference
struct rect{
		int topx,topy,botx,boty;	
};
The above has already been declared please do not redclare */

if((((ra.topx - rb.botx) ^ (ra.boty-rb.topx)) &
((ra.topy - rb.boty) ^ (ra.boty-rb.topy))) <0)
{return 1;}
return 0;
}

I hope the above would work

- Anirudh Kumar June 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

detect when they re not overlapped.

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

stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other

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

silentmatt.com/rectangle-intersection/ see this too!!

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

write line equation for all sides of two rectangles... match each line against each line of the other rectangle for intersection.... dealing with equations is a cleaner way....

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

It is possible that the rectangles are rotated so it is not sufficient to check the bounds defined by the minimum and maximum values of x and y.

We can use a concept called the Minkowski Sum to determine if our rectangles overlap.

For our purpose, we want to take polygon A + -polygon B, so for convenience I will call this the Minkowski difference.

If we take every point in polygon A and subtract every point in polygon B we get a new polygon. If this polygon contains the origin then A and B overlap.

In this example we would end up with 16 points.

There is a generalized algorithm for finding if our new polygon contains the origin, but since we are dealing with rectangles we can us a trick to simplify the process.

We find an additional point by taking the Minkowski difference of the centers of the rectangles. Lets call this Mc and the vector from Mc to the origin as Mc->O.

We can use dot product of the Mc->O, and the vector defined by Mc and each point in our Minkowski difference to determine the distance of each point. to the origin, in the direction of Mc->O.

If the any of distance to any of our points is greater than the magnitude of Mc->O then our rectangles overlap.

We can, actually, speed this up by taking the maximum of the dot product of each point in A with Mc->0 plus the maximum of the dot product of each point of B with O->Mc and comparing this to the length of Mc->0. This cuts the number of dot products in half.

- JIm Daleo November 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

detect when they re not overlapped. t

- Anonymous February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Here's a clean solution.
Rect 1 say represented in points Xi, Yi (get equation of all four lines in form Y=mx +c)
for each line in rectangle 1

{
for each point in rectangle 2
{
check if any point lies on different sides of two opposite lines pairwise.
if it does then it is an interior point in the rectangle;
break;
}

}

a point is on different sides of a line can be found easily as follows.

put the point on the line equation. of L1 and L2 if they give result in same sign (+ or -) they lie on same side, else they are on diff side

- Amit Priyadarshi March 18, 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