Microsoft Interview Question for Software Engineer / Developers






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

public boolean intersects(Rectangle r, Rectangle r1) {
int tw = r1.width;
int th = r1.height;
int rw = r.width;
int rh = r.height;
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
return false;
}
int tx = r1.x;
int ty = r1.y;
int rx = r.x;
int ry = r.y;
rw += rx;
rh += ry;
tw += tx;
th += ty;
// overflow || intersect
return ((rw < rx || rw > tx) &&
(rh < ry || rh > ty) &&
(tw < tx || tw > rx) &&
(th < ty || th > ry));
}

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

public class Rectangle
{
int x, int y, int width, int height;
}

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

I would use the centers of the rectangles. Let h1, w1 denote height and width of the first rectangle and so on. Then if the difference between y coordinates of the centers is smaller than (h1+h2)/2 they intersect. Similarly if the difference between the x coordinates of the centers is smaller than (w1+w2)/2 they intersect

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

Almost. You can't assume they intersect if one condition is fulfilled or the other. Your example would pass if two rectangles had centers at the same y value but were very far apart from each other and never intersected. You would need to verify that |y2-y1| <= (h1+h2)/2 AND |x2-x1| <= (w1+w2)/2.

- woohoo March 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <math.h>
#define _X 1
#define _Y 0

typedef struct rectangle {
  int x1; /* bottom side of rectangle */
  int y2; /* left side of rectangle */
  int width;
  int height;
} rectangle;

int get_center(rectangle *r, int is_x) {
  if (r == NULL)
    return 0;
  if (is_x)
    return ((r->x1+r->width)/2);
  else
    return ((r->y1+r->height)/2);
}

int intersecting(rectangle *r1, rectangle *r2) {

  int r1cx, r1cy, r2cx, r2cy;

  if (r1 == NULL || r2 == NULL)
    return;

  r1cx = get_center(r1, IS_X);
  r2cx = get_center(r1, IS_X);
  r1cy = get_center(r2, IS_Y);
  r1cy = get_center(r2, IS_Y);

  if ((abs(r1cx-r2cx) <= (r1->width+r2->width)/2) && \
      (abs(r1cy-r2cy) <= (r1->height+r2->height)/2))
    return true;
  return false;
}

pardon small typos if there are any; didn't compile this.

- woohoo March 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if rectangles are not parallel to the coordinate axes ?

- Anonymous March 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. For each edge of rectangle1 try to find if the verteces of rectangle1 and rectangle2 are on the opposite sides.
2. If any such edge exists then the rectangles dont intersect.

Note : It wont work if rectangles are concentric. May be we'll have to handle that seperately.

- insigniya March 13, 2011 | Flag


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