## 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...

Comment hidden because of low score. Click to expand.
0

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

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 );
}``````

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]

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.

### 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.