Interview Questions

Write a function that takes in two rectangles and returns true if the overlap and false if they do not.

Software QA/Tests Interview Questions from Microsoft


(Continued from previous question...)

Write a function that takes in two rectangles and returns true if the overlap and false if they do not.

Q:
Microsoft Interview Question for Software Engineer in Tests about Coding

Write a function that takes in two rectangles and returns true if the overlap and false if they do not.

A:
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;
}

(Continued on next question...)

Other Interview Questions