## Microsoft Interview Question for Software Engineer / Developers

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

Definition:
===========
The union of two rectangles is the smallest single rectangle that completely covers both of the areas covered by the two given rectangles.

Let,
Co-ordinates of Rectangle 1 be,
left top (x1,y1),
right bottom (x2,y2)
Co-ordinates of Rectangle 2 be,
left top (x3,y3),
right bottom (x4,y4)
And x increases when going right an y increases when going up

Co-ordinate of the new union'ed rectangle,
left top (min(x1,x3),max(y1,y3))
right bottom (max(x2,x4),min(y2,y4))

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

wrong. you didn't consider from which side another rectangle will interfere with the one

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

Input: widths, heights, top-left coordinates.

Position the top-left corners of the rects at the origin(widths and heights already known for both rects so no computation necessary). Then the final rect is (max(w1,w2),max(h1,h2)).

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

Can you please explain what is max and min?? a little elaboration might be good enough if you can..thanks

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

Aren't Co-ordinate of the new union'ed rectangle?
left top (max(x1,x3),min(y1,y3))
right bottom (min(x2,x4),max(y2,y4))

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

max = maximum of two numbers
min = minimum of two numbers

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

I dont think that definition is correct. there is no guarantee that the union is a rectangle. union means shape that covers both rentangles.

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

Should it be the area of union? The real union is hard to return if
Two rects do not have any intersection;
Union of two rects is a ploygon; ...
Anyway I don't follow this question

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

you have an intersection answer in previous interviews.
there is an answer writeen by James. i think its very similar, except that the interior points sould be replaced by exterior points.

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

There is a big problem in all the above answers, whether finding intersection or union, whatever you want to call it. It'll fail when none of the corners of the rectangles is inside the other.

for example
--------
| |
-----|--- |
| | | |
-----|--- |
--------

or even more difficult

----
| |
-------------
| | | |
-------------
| |
----

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

I think david's solve will works.

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

As Eila pointed out, we have intersection of the two rectangles then, Union (A,B) = A + B - Intersection(A,B).
This should solve the problem.

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

careercup.com/question/?q=7a052c02-eacf-4fd0-b029-75c4da495ba6

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

Are their sides supposed to be parallel?

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

The fefinition of the "union of rectangles" is accurate in the 1st post:
this is the smallest rectangle which includes both our rectangles within it.

Given:
R1: (x1,y1),(x2,y2);
R2: (x3,y3),(x4,y4);

No assumptions about their orientations and coordinate system is needed (i.e. x1>x2 or vise versa), still we have:

left: min(x1,x2,x3,x4);
right: max(x1,x2,x3,x4);
top: min(y1,y2,y3,y4);
bottom: max(y1,y2,y3,y4).

Union: (left,bottom), (right, top)

.

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

monte carlo method

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.