Microsoft Interview Question for Software Engineer in Tests






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

return ! ( (R1.right<=R2.left) | (R2.right<=R1.left) | (R1.bottom<=R2.top) | (R2.bottom<=R1.top) )

- N October 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesn't work for :-

+-------+
 |       |
 +-------+
     +-------+
     |       |
     +-------+

- MikeZ November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You are assuming that the rectangle is aligned with axes.. It could be more generic where you have total 8 pairs of coordinates ( 4 for each ).

- MN October 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

find distance betweeen the centers of two rectangles(let it be D)
find distance of centre of R2 from edge of R1(let it be s1)
" " " " " R1 from edge of R2(let it be s2)
if s1+s2!= D then rectangles overlap

- Anonymous October 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think condition should be (s1+s2)<=D

- Aditya October 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I could not understand the solution......
"distance of centre of R2 from edge of R1(let it be s1)" !!!! From which edge of R1 !!!!
similarly for R1

- Piyush October 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For any Rectangle find its four corresponding lines using y-y1/(x-x1) = y2-y1/x2-x1
You got four lines for Rectangle A.

You got four points for rectangle B.

For A or B to intersect ( I am assuming this is what overlap means), the value of one point should be > 0 and < 0 for two parallel lines and < 0 and > 0 for the other two parallels lines. ( or the other way around)

In case not they would have the same sign. ( both > 0 and both < 0 or other way around).

- ekapil2 October 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes
Doesn't work for :- {{{ +---------------+ | | | +------+ | | | | | | +------+ | +---------------+ - MikeZ November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry for the bad sketch, I was trying to draw this

+---------------+
|               |
| +------+      |
| |      |      |
| +------+      |
+---------------+

- MikeZ November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am not sure what does overlap means here. Is it that its contained in an array or is it that it cuts the boundary or something.

In case of former( which is the figure you have drawn).
For each point of Rect A( inside Rect).
The product of (topline * bottomLine) < 0 and ( leftLine * RightLine < 0)

This should be true for all points.

- eName November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Overlapping = Atleast one vertex of a rectangle contained within/on the vertices of the other.

My method - Check each vertex of each rectangle for containment within the other.

bool isContained( rect r, point p)
{
   if( (p.x > r.left && p.x < r.right) //Check if x is within rect's bounds
       && (p.y < r.top && p.y > r.bottom) )
   { 
      return true;
   }
   else
      return false;
}

bool isOverlapped( rect bg_rect, rect fg_rect)
{
   for each (vertex v in fg_rect)
   { 
       if (isContained(bg_rect, v))
       { 
           return true;
       }
    }
    return false;
}

bool checkMutualOverlap(rect r1, rect r2)
{
   return (isOverlapped(r1,r2) || isOverlapped(r2,r1));
}

- MikeZ November 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

All '<'s should've been '<=' s

- MikeZ November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

doesn't work in the case

+-----------+
+-----|-----------|-------+
| | | |
+-----|-----------|-------+
+-----------+

as no vertex is contained inside other rectangle.

- Manishi Singh November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I wanted to draw:

+-----------+
+-----|-----------|-------+
|             |                          |                 |
+-----|-----------|-------+
              +-----------+

- Manishi Singh November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other

- abhi January 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

project the 2 rectangles into the X-axis and Y-axis, then check whether they are overlap on both axes.

- Anonymous February 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

NA

- weijiang2009 February 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct
{
double x;
double y;
double width;
double height;
} rectangle;

bool overlap(rectangle *rect_a, rectangle *rect_b)
{
bool var = false;

if (rect_a->x<=rect_b->x && rect_a->y<=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_a->width &&
abs(rect_a->y-rect_b->y)<=rect_a->height)
var = true;
}
else if (rect_a->x>=rect_b->x && rect_a->y<=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_b->width &&
abs(rect_a->y-rect_b->y)<=rect_a->height)
var = true;
}
else if (rect_a->x>=rect_b->x && rect_a->y>=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_b->width &&
abs(rect_a->y-rect_b->y)<=rect_b->height)
var = true;
}
else if (rect_a->x<=rect_b->x && rect_a->y>=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_a->width &&
abs(rect_a->y-rect_b->y)<=rect_b->height)
var = true;
}
else
{
var = true;
}

return var;
}


Can someone help to debug/test/improve my logic? Thanks.

- Anonymous June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct
{
double x;
double y;
double width;
double height;
} rectangle;

bool overlap(rectangle *rect_a, rectangle *rect_b)
{
bool var = false;

if (rect_a->x<=rect_b->x && rect_a->y<=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_a->width &&
abs(rect_a->y-rect_b->y)<=rect_a->height)
var = true;
}
else if (rect_a->x>=rect_b->x && rect_a->y<=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_b->width &&
abs(rect_a->y-rect_b->y)<=rect_a->height)
var = true;
}
else if (rect_a->x>=rect_b->x && rect_a->y>=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_b->width &&
abs(rect_a->y-rect_b->y)<=rect_b->height)
var = true;
}
else if (rect_a->x<=rect_b->x && rect_a->y>=rect_b->y)
{
if(abs(rect_a->x-rect_b->x)<=rect_a->width &&
abs(rect_a->y-rect_b->y)<=rect_b->height)
var = true;
}
else
{
var = true;
}

return var;
}


Can someone help to debug/test/improve my logic? Thanks.

- zhou June 14, 2011 | 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