Amazon Interview Question for Applications Developers


Country: United States
Interview Type: Written Test




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

Do it this way .. Make a rectangle d covering both the rectangles.. the length and width of this new rectangle has to be less than the sum of both the rectangles for any overlap

int doesRectOverlap(rect ra, rect rb){ 
	rect d;
	d.topx = min(ra.topx,rb.topx);
	d.topy = min(ra.topy,rb.topy);
	d.botx = max(ra.botx, rb.botx);
	d.boty = max(ra.boty,rb.boty);
     if(width(d) > width(ra)+ width(rb) || length(d) > length(ra) + length(rb))
	return 0;
	else
return 1; 
}

/* For your reference 
struct rect{ 
int topx,topy,botx,boty; 
};

- kkr.ashish January 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good approach, I think the following correction is needed:
d.topy = max(ra.topy, rb.topy);
d.boty = min(ra.boty, rb.boty);

- nharryp March 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if the rectangles are rotated 45 degrees, i.e.

.      .
       / \    / \
      /   \ /     \
      \   / \     /
       \ /   \  /

- foo May 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

int doesRectOverlap(rect ra, rect rb){ 
	if ((rb.topy < ra.boty) || (ra.topy < rb.boty) || (rb.topx > ra.botx) || (ra.topx > rb.botx))
		return 0; do not overlap
	return 1; // do overlap.
}

- Anonymous January 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int doesRectOverlap(rect ra, rect rb){
if ((rb.topy < ra.boty) || (ra.topy < rb.boty) || (rb.topx < ra.botx) || (ra.topx < rb.botx))
return 0; do not overlap
return 1; // do overlap.
}

- ZhenyiLuo January 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are both wrong, you want to consider both horizonal and vertical;

bool doesRectOverlap(rect ra, rect rb)
{ 
bool ret=0;
ret|=(ra.topx<rb.topx)&&(ra.botx>rb.topx)&&(ra.topy<rb.topy)&&(ra.boty>rb.topy);
ret|=(ra.topx<rb.topx)&&(ra.botx>rb.topx)&&(ra.topy<rb.boty)&&(ra.boty>rb.boty);
ret|=(ra.topx<rb.botx)&&(ra.botx>rb.botx)&&(ra.topy<rb.topy)&&(ra.boty>rb.topy);
ret|=(ra.topx<rb.botx)&&(ra.botx>rb.botx)&&(ra.topy<rb.boty)&&(ra.boty>rb.boty);
return ret;
}

- Charles January 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Anonymous, ZhenyiLuo: Your algorithm is flawed.

You need to check:
"Do the rectangles overlap vertically?" AND "Do the rectangles overlap horizontally?". You can't just do one or the other.

- Arxo Clay January 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int doesRectOverlap(rect ra, rect rb){
if ( rb.topy > ra.boty || rb.boty < ra.topy || rb.topx > ra.botx || rb.botx < ra.topx)
return 0; do not overlap
return 1; // do overlap.
}

- Dave January 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int doesrect(struct rect ra,struct rect rb)
{
    struct rect temp;
    temp.topx = (ra.topx<rb.topx)?ra.topx:rb.topx;
    temp.topy = (ra.topy<rb.topy)?rb.topy:ra.topy;
    temp.botx = (ra.botx<rb.botx)?rb.botx:ra.botx;
    temp.boty = (ra.boty<rb.boty)?ra.boty:rb.boty;
    if(((temp.botx-temp.topx)>(ra.botx-ra.topx + rb.botx-rb.topx)) || ((temp.topy-temp.boty)>(ra.topy-ra.boty + rb.topy-rb.boty)))
       return 0;
    return 1;
}

- Nitin February 11, 2014 | 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