Amazon Interview Question Software Engineer / Developers


Country: India


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

Rec1 = {(x1, y1), (x2, y2)}
Rec2 = {(x3, y3), (x4, y4)}

if (x2 <= x3 || x4 <= x1 || y3 <= y2 || y1 <= y4) {
    return -1;
}
return 1;

- Jim on August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correcto. Came up with the same.

- DeathEater on August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will work only if the rectangles are axis aligned. If they aren't this will fail. For ex: consider rectangle1{(0,2) (5,0)} and rectangle2{(10,10),(-11,-9)}.

- Second Attempt on August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It wont work even for same axis aligned....It should be
if (x2 <= x3 || x4 <= x1 || y3 >= y2 || y1 >= y4) {
return -1;
}
return 1;

- Anonymous on August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the original version is correct, at least more correct than the modified version that Anonymous proposed. This is assuming that y2 is the bottom of 1st rectangle while y3 is the top of the 2nd rectangle. So if y3 <= y2 then they don't overlap, not the other way around.

- Sunny on January 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

package amazon;
/*
* coordinate system:
* -<--------0----------------->X+
* |
* |
* |
* | +
* |
* |
* v
* Y+
*
* */
class Rectangle{
public int topleftX;
public int topleftY;
public int bottomrightX;
public int bottomrightY;
public Rectangle(int x1,int y1,int x2,int y2){
if(x2>x1&&y2>y1){
this.topleftX=x1;
this.topleftY=y1;
this.bottomrightX=x2;
this.bottomrightY=y2;
}
else{
System.out.println("err");
return;
}
}

}

public class Check_Two_Rectangle_Has_Common_Area {

public static int hasCommonArea(Rectangle o1,Rectangle o2){

int x1=o1.topleftX;
int y1=o1.topleftY;
int x2=o1.bottomrightX;
int y2=o1.bottomrightY;

int x3=o2.topleftX;
int y3=o2.topleftY;
int x4=o2.bottomrightX;
int y4=o2.bottomrightY;

if(y4<=y1){
return -1; //o2 above o1, totally
}
if(y3>=y2){
return -1; //o2 below o1, totally
}
if(x4<=x1){
return -1; //o2 left o1, totally
}
if(x3>=x2){
return -1; //o2 right o1, totally
}

return 1;

}
public static void main(String[] args) {
Rectangle o1=new Rectangle(0,0,1,1);
Rectangle o2=new Rectangle(2,2,10,10);
System.out.println(hasCommonArea(o1,o2));


}

}

- Chengyun Zuo on August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I believe there should not be a <= or >= condition. It should be < or > condition only. Reason is, how you will figure out if one rectangle just sits over another rectangle. I mean, all the coordinates are same?

- hari@hbiz.in on August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include <cstdio>

using namespace std ;

class rectangle {
  public :
    int x1;
    int x2, y1, y2;
    rectangle (int x_1, int y_1, int x_2, int y_2) {
      x1 = x_1 ;
      x2 = x_2 ;
      y1 = y_1 ;
      y2 = y_2 ;
    }
};
int sharedArea (rectangle rec1, rectangle rec2) {
  if (( rec1.x1 > rec2.x1 && rec1.x1 > rec2.x2 ) && ( rec1.x2 > rec2.x1 && rec1.x2 > rec2.x2 )) {
    printf ( "\nNot matching rect1 is right of rect2" ) ;
    return -1 ;
  }
  if (( rec1.x1 < rec2.x1 && rec1.x1 < rec2.x2 ) && ( rec1.x2 < rec2.x1 && rec1.x2 < rec2.x2 )) {
    printf ( "\nNot matching rect1 is left of rect2" ) ;
    return -1 ;
  }
  if ((rec1.y1 > rec2.y1 && rec1.y1 > rec2.y2 ) && ( rec1.y2 > rec2.y1 && rec1.y2 > rec2.y2 )) {
    printf ( "\nNot matching rect1 is bottom of rect2" ) ;
    return -1 ;
  }
  if ((rec1.y1 < rec2.y1 && rec1.y1 < rec2.y2 ) && ( rec1.y2 < rec2.y1 && rec1.y2 < rec2.y2 )) {
    printf ( "\nNot matching rect1 is up of rect2" ) ;
    return -1 ;
  }

  return 1 ;
}
int main () {
  rectangle r1 (0,0,6,5) ;
  rectangle r2 (5,4,8,-5) ;
  if ( sharedArea (r1,r2) == 0 )
    printf ( "\nNo shared area" ) ;
  else printf ( "\nHas shared area" ) ;
  return 0;
}

- Psycho on August 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int isCommonArea(Rectangle rectOne,Rectangle rectTwo)
        {
                int x1 = rectOne.top_left_x;
                int y1 = rectOne.top_left_y;
                int x2 = rectOne.right_bottom_x;
                int y2 = rectOne.right_bottom_y;
                
 
                int x3 = rectTwo.top_left_x;
                int y3 = rectTwo.top_left_y;
                int x4 = rectTwo.right_bottom_x;
                int y4 = rectTwo.right_bottom_y;
                
                if(x1<x3 && x3<x2 && y1>y3 && y3>y2)
                        return 1;
                if(x1<x3 && x3<x2 && y1>y4 && y4>y2)
                        return 1;
                if(x1<x4 && x4<x2 && y1>y3 && y3>y2)
                        return 1;
                if(x1<x4 && x4<x2 && y1>y4 && y4>y2)
                        return 1;
                return -1;
        }

For Full Code: ideone.com/yyE68

- cobra on August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even if this is correct (I haven't checked), it's way more complicated than it needs to be.

- Anonymous on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

understand the program first, then you will know that it is not much complicated as you are thinking..

- cobra on August 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int rect_intersect(struct rect ra, struct rect rb)
{
val1= (ra.topx - rb.botx) * (ra.botx-rb.topx);
val2= (ra.topy - rb.boty) * (ra.boty-rb.topy);
if(val1<0 && val2<0)
return 1;
else return -1;
}

- Preety on August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Where does it say that edges are parallel to the co-ordinates? All solutions are based on this assumption.

- dc360 on August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practicepackage;

public class CheckSimilarAreasOfRectangles{

	int x1;
	int x2;
	int x3;
	int x4;

	int y1;
	int y2;
	int y3;
	int y4;
	
	public void getCordinatesofFirstRectangle(int x1,int y1,int x2,int y2)
	{
		this.x1=x1;
		this.y1=y1;
		this.x2=x2;
		this.y2=y2;
	}

	public void getCordinatesOfSecondRectangle(int x3,int y3,int x4,int y4)
	{
		this.x3=x3;
		this.y3=y3;
		this.x4=x4;
		this.y4=y4;
	}
	
	public int calculateAreaOfRectangles()
	{
		int firstRectangleHeight=y2-y1;
		if(firstRectangleHeight<0)
			firstRectangleHeight*=-1;
		int firstRectangleLength=x2-x1;
		if(firstRectangleLength<0)
			firstRectangleLength*=-1;
		int areaFirstRectangle= firstRectangleHeight*firstRectangleLength;

		int secondRectangleHeight=y3-y4;
		if(secondRectangleHeight<0)
			secondRectangleHeight*=-1;
		int secondRectangleLength=x4-x3;
		if(secondRectangleLength<0)
			secondRectangleLength*=-1;
		int areaSecondRectangle= secondRectangleHeight*secondRectangleLength;
		
	
		if(areaFirstRectangle==areaSecondRectangle)
			return 1;
		else 
			return -1;	
	}
	
	public static void main(String[] args)
	{
		CheckSimilarAreasOfRectangles sar= new CheckSimilarAreasOfRectangles();
		sar.getCordinatesofFirstRectangle(5,5,8,2);
		sar.getCordinatesOfSecondRectangle(13,10,16,7);
		int equality = sar.calculateAreaOfRectangles();
		System.out.println("The rectangles are equal in areas if 1 or are unequal if -1");
		System.out.println("The equality number for areas of two rectangles is: "+ equality);
	}
}

- codingAddicted on August 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

two rectangles are overlapped only if when projections to x and y axis are both overlapped. so in the non-rotate case, i.e. assume rect A[(xa0, ya0), (xa1, ya1)], B[(xb0, yb0), (xb1, yb1)] both aligned to the axis,
then
length_a = xa1 - xa0,
length_b = xb1 - xb0,
max_length_interval = max(abs(xb1 - xa0) , abs(xb0 - xa1)),
ditto for height; so they overlap
if (max_length_interval <= length_a + length_b) && (max_height_interval <= height_a + height_b)

- john on September 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find out distance between two point.and if both have same o/p means.both have same area.
formula = sqrt ((x2-x1)*(x2-x1)-(y2-y1)((y2-y1)).

Thanks

- Raj on April 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Lets say (Rec = rectangular, L = Line)
Rec1 = (PintTop(X1t, Y1t), PointBottom(X1b, Y1b))
and
Rec2 = (PintTop(X2t, Y2t), PointBottom(X2b, Y2b))
L1x = (Pint(X1t, Y1t), Pint(X1b, Y1t))
L2x = (Pint(X2t, Y2t), Pint(X2b, Y2t))
L1y = (Pint(X1t, Y1t), Pint(X1t, Y1b))
L2y = (Pint(X2t, Y2t), Pint(X2t, Y2b))

so you simple check if lines intersect:

if (L1x intersect L2x && L1y intersect L2y)
return 1
else
return -1

- GKalchev on August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is a Java method for checking if there is intersection:

private static boolean intersect (int a1, int b1, int a2, int b2)  {
		boolean result;
		if (a1 > a2)
			if (a1 > b2)
				result = false;
			else
				result = true;
		else
			if (b1 >= a2)
				result = true;
			else
				result = false;
				
		return result;
	}

- GKalchev on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This makes very little sense to me.

- Anonymous on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

1. Take mod of top left x and bottom right x for first rectangle. 2.take mod of top left y and bottom right y. 3.multiply ans of 1 and 2. Repeat the steps for rectangle. And compare area of both.

- Ankur garg on August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This wont work if the rectangles are rotated... it works only if the rectangles are aligned to the axis

- Anonymous on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Given how this question is phrased I would take it that the rectangles are axis-aligned

- Anonymous on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If they were aligned to the axis then this problem would be too easy. You could easily figure out the dimensions of the rectangle, then then it would just be child's play to find if boundaries were inside each other.

- Anon on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually, I take that back. I don't think it's possible to define a rectangle with 2 corner points and no reference axis.

- Anon on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also, it's worth keeping in mind that many interview questions are easy. So this could just be an easy question.

- Anonymous on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

return rec1.containsAnyCornerOf(rec2)|| rec2.containsAnyCornerOf(rec1)

PointToLineDistance(Point point,Line line)

- Vincent on August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess you should then indicate how to implement containsAnyCorner.

- Anonymous on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

There is an infinite number of rectangles with given left most and rightmost corners.

- florin314 on August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

man given these 2 co-ordinates we can uniquely draw a rectangle...

- atul gupta on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's only unique if you know the axis which they perpendicular to. Otherwise. take two opposite endpoints points of a rectangle (non-square). I can always draw a square with the same endpoint coordinates on a different set of axis

- Anon on August 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If the diagonals of the two rectangles are equal then they would be of equal area. So just compare diagonal lengths.

- Ankur garg on August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's not true, nor is that the question being asked

- Anon on August 08, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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