## Amazon Interview Question

Quality Assurance EngineersI doubt whether this works for all cases

Consider square 1 with topleft(1,3) and bottom right(3,1)

square 2 with topleft(2,4) and bottom right(4,2)

both these squares intersect at 2 points

by your algorithm

x1=max(1,2)=2

y1=max(3,4)=4

x2=min(3,4)=3

y2=min(1,2)=1

x2>x1 is true

but y2>y1 is false

but still they intersect

Its pretty simple actually - just try to verify that they do NOT intersect:

- Let the boxes (rectangles) be represented by two points each. Let the points be (a,b) and (c,d) for first and (w,x) and(y,z) for the second.

- Check for all four cases where the cannot intersect:

> One box is to the left of other - its a & c will be less than w and x of the other

> One box is to the right of the other, its a & c will be greater than w and x of the other

> One box is vertically higher than other

> or the first box is vertically lower than the other

//Here is code:

bool DoIntersect(Rectangle rectFirst, Rectangle rectSecond)

{

int a = rectFirst.bottomLeft.X;

int b = rectFirst.bottomLeft.Y;

int c = rectFirst.topRight.X;

int d = rectFirst.topRight.Y;

int w = rectSecond.bottomLeft.X;

int x = rectSecond.bottomLeft.Y;

int y = rectSecond.topRight.X;

int z = rectSecond.topRight.Y;

// checking for x co-ordinates

if(a<w && c<w && a<y && c<y) return false;

if(a>w && c>w && a>y && c>y) return false;

// checking for y co-ordinates

if(b<x && d<x && b<z && d<z) return false;

if(b>x && d>x && b>z && d>z) return false;

return true;

}

Take the area of the squares and the screen.

If Area(Screen) - Area(Square1) = Area(Square2), then there is no intersection.

Else, there is an intersection.

Think of it this way. Imagine two squares that do not intersect. Then if you subtract the area of the screen from the area of the square you should get the area of square 2.

If they intersect, subtracting one of the squares from the area of the screen will result in a value less than the other square since a portion of the square would have been taken out.

This took me a while to solve haha.

Two squares, if they intersect, at least one of the point of one square is inside the other.

Do following steps:

1. take two adjacent points (A and B) of a square (S1), calculate the vector which perpendicular to the edge and point out, say v1.

2. take one point (P) from the other square (S2), calculate the vector of PA and PB, say v2

3. calculate the dot product of v1 and v2.

4. repeat 1-3 (for all adjacent points of S1, if the point is inside the square, then all the dot products will be positive or 0.

geek for geek

# include<stdio.h>

# include<stdlib.h>

# define bool int

/* Function to find minimum of 3 numbers */

unsigned min(unsigned , unsigned , unsigned );

/* Function to get the nth ugly number*/

unsigned getNthUglyNo(unsigned n)

{

unsigned *ugly =

(unsigned *)(malloc (sizeof(unsigned)*n));

unsigned i2 = 0, i3 = 0, i5 = 0;

unsigned i;

unsigned next_multiple_of_2 = 2;

unsigned next_multiple_of_3 = 3;

unsigned next_multiple_of_5 = 5;

unsigned next_ugly_no = 1;

*(ugly+0) = 1;

for(i=1; i<n; i++)

{

next_ugly_no = min(next_multiple_of_2,

next_multiple_of_3,

next_multiple_of_5);

*(ugly+i) = next_ugly_no;

if(next_ugly_no == next_multiple_of_2)

{

i2 = i2+1;

next_multiple_of_2 = *(ugly+i2)*2;

}

if(next_ugly_no == next_multiple_of_3)

{

i3 = i3+1;

next_multiple_of_3 = *(ugly+i3)*3;

}

if(next_ugly_no == next_multiple_of_5)

{

i5 = i5+1;

next_multiple_of_5 = *(ugly+i5)*5;

}

} /*End of for loop (i=1; i<n; i++) */

return next_ugly_no;

}

/* Function to find minimum of 3 numbers */

unsigned min(unsigned a, unsigned b, unsigned c)

{

if(a <= b)

{

if(a <= c)

return a;

else

return c;

}

if(b <= c)

return b;

else

return c;

}

/* Driver program to test above functions */

int main()

{

unsigned no = getNthUglyNo(150);

printf("%dth ugly no. is %d ", 150, no);

getchar();

return 0;

}

we have the top left & bottom right coordinated of the sqaures..

- Anonymous June 20, 2011find :

int x1 = max(topleft_first_sq_x,topleft_sec_sq_x);

int y1 = max(topleft_first_sq_y,topleft_sec_sq_y);

int x2 = min(bottomright_first_sq_x,bottomright_sec_sq_x);

int y2 = min(bottomright_first_sq_y,bottomright_sec_sq_y);

if (x2>x1 && y2>y1)

they intersect

else

they dont