Interview Question for SDE-2s


Country: India
Interview Type: Written Test




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

Ok, so you get top left and bottom right coordinates for two rectangles?
Call top left point, "U" and bottom right "L"

// if either rectangle is totally to the right of the other, return false:
if( rec1.U.x > rec2.L.x || rec2.U.x > rec1.L.x) return false;

// if either rectangle is totally above the other, return false ...
if( rec1.L.y > rec2.U.y || rec2.L.y > rec1.U.y) return false;

// else return true ...
return true;

Someone verify/check this...

- S O U N D W A V E September 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Verified. Alternatively you can do a positive check to find if the rectangles overlap. Check my comment.

- siva September 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean overlap( Rect a, Rect b){
return( a.ul.x <= b.lr.x &&
	a.ul.y >= b.lr.y &&
	a.lr.x >= b.ul.x &&
	a.lr.y <= b.ul.y );
}

- siva September 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Consider each of these diagonal values as intervals. Two intervals i and j overlap if:

Lower[i]<=Upper[j] && Lower[j]<=Upper[i]

- Anonymous October 07, 2013 | 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