Adobe Interview Question for Member Technical Staffs


Team: InDesign
Country: United States
Interview Type: In-Person




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

are the rectangles axis-aligned?

- tom.thkwok October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we can consider that the rectangles are axis-aligned .

- nk October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume existing rectangles are represented by x,y s of diagonal
Randomly pick two more a,b so that this condition isn't true:
A doesn't lie between x1 and |x1-x2|. AND b doesn't lie between y1 and |y1-y2|

- Sat October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For two rectanles A and B
A : (AX1,AY1,AX2,AY2)
B : (BX1,BY1,BX2,BY2)

{
if(AX2<BX1||AY2<BYW||AX1>BX2||AY2>BY2)
    No Intersect
ELSE
    Intersect

}

- DarkKnight October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@all :-
u have to minimise the number of comparisons
and there are multiple rectangles with co-ordinates given
(top,left,bottom,right)
Plz note - "minimise the number of comparisons "

- nk October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

let one rectangle be represented by top-left coordinate 'TL' (x1,y2) and bottom-right 'BR'(x2,y2).
the other to be 'tl'(a1,b1) and 'br'(a2,b2) respectively.

Key: we only need to make sure either br is not at the bottom-left section of TL ==OR== tl is not at the top-left section of BR.

# of comparison: following code is a all OR check, any one comparison starting from right hand side that gave 'true' will terminate the rest comparisons and directly go to print. so worst case will need 4*n comparison with average 2*n comparison needed.

code:

if (  (a2<x1||b2<y1)   ||   (a1>x2||b1>y2)  )    {NO Intersection; Print rect;}
            br <  TL  ==OR==  tl > BR

- Ares.Luo.T October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The coordinates of left , right, top, bottom are in windows coordinate systems with origin at top left.

struct Rect
{
long left;
long right;
long top;
long bottom;
};

bool IntersectRect(Rect *R1, Rect *R2)
{
return (R2->left > R1->right || R2->right < R1->left || R2->top > R1->bottom || R2->bottom < R1->top);
}

If the rectangle intersects then the function IntersectRect returns false other wise true.

we can have have an array of all the rectangles drawn on the screen and shall compare all the drawn rectangles with the new rectangle to be drawn. If it intersect with any of the rectangles we shall not draw it.

- Shyam Gupta October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why everybody is assuming that there are only 2 rectangles already drawn on the screen.

- Nitin Gupta October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Lets say that all rectangles are aligned on x-axis. And new rectangle is also aligned on x-axis.

1. Maintain a sorted array of x1 and a sorted array of x2 for each rectangle printed. (maintaining x dimensions only is enough for x-axis aligned rectangles)
2. For every i in x1[i] will belong to same rectangle in x2[i] because rectangles never overlap.
3. If new rectangle is (a1,b1) and (a2,b2). Then binary search a1 and a2 in x1 and x2 if a match is found then do not print rectangle. Otherwise find closest values (x1[m] and x2[n]) of a1 and a2.
4. if m==n then we have one closest rectangle to compare with it.
5. if m!=n then there would be two rectangles at the most (x1[m],x2[m]) and (x1[n],x2[n]) to compare the overlapping.

Note: since all rectangles are x-axis aligned so for overlapping comparison we need to compare only x1 and x2 of new rectangle. Ignore y1 and y2.

- susheel October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

updated the Note
Note: since all rectangles are x-axis aligned so for overlapping comparison we need to compare only a1 and a2 of new rectangle. Ignore b1 and b2.

- susheel October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Susheel - I think you are wrong. Even if two rectangles have same x1 and x2, they can still be non-intersecting based on their y co-ordinate.

For ex.
Rectangles with bottom left (BL) and top right (TR) co-ordinate.

Rectangle 1 - BL = (3,1) TR = (6,4)
Rectangle 2 - BL = (3, 5) TR = (6, 8)

As you see both rectangles have same x co-ordinates but still if you draw them on cartesian-plane, they will be non-intersecting.

- Nitin Gupta October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Susheel, my observations here - I think you would need to maintain two sorted arrays ( one by x and one by y )

Try to insert the new rectangle into these sorted arrays.

Intersection will happen when
New Rectangle N overlaps with same rectangle R from existing list on both x and y axis.

Note that trying to insert this rectangle into the sorted array is O(N) operation.

So I am not sure if this method is any better than just checking against each rectangle by simple method.

Thoughts ?

- DarkKnight October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If we assume that rectangles are X-axis aligned, we just need to check if their bottom sides overlap. So problem minimizes to line overlapping instead of rectangle overlapping.

We will keep two sorted arrays noting X coordinates of start and end points of bottom arm of rectangle.
say start[] and end []

Let bottom side of new rectangle be denoted by newstart, newend. Then algorithm can be :-

for (i=n)
    if ( newstart < end[i] ) 
           { if ( newend > start [i] )
                  //overlapping so exit
             else
                  // not overlapping so continue with next line
                  i--
             }
     else
        {   // line being compared is finished bfore new one. so no over lapping. 
          Print line. 
          Break loop
        }

- Kaushik Lele October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Nitin,
If the rectangles are aligned on x axis, how can BL be (3,1).. any bottom y-coordinate of x-axis aligned rectangle is 0. So the BL will always be (3,0) or in the form (Xi,0).

Hi DarkKnight,
because y-coordinate of bottom points is always 0, comparing x is always enough.
Also searching the position of new coordinates is O(log n), which is minimizing comparisons as asked in the question.

It is not mentioned that rectangles will keep coming, even if that is the case then we can use sorted ArrayList (which internally uses gap array kind of implementation) for efficient insertions.

- susheel October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Susheel,

how can we make the assumption that "because y-coordinate of bottom points is always 0". The question says the rectangles are axis aligned, which means that the sides are parallel to the axis. Not necessarily on the axis.

I think you make a fair point that if we only have to check for 1 rectangle we can do it in log(n) time.

But note that sorting will take at o(nlogn) time.

So how is this method better than checking the new rectangle against each existing rectangle which will take O(n) time

- DarkKnight October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we assume that rectangles are X-axis aligned, we just need to check if their bottom sides overlap. So problem minimizes to line overlapping instead of rectangle overlapping.

We will keep two sorted arrays noting X coordinates of start and end points of bottom arm of rectangle.
say start[] and end []

Let bottom side of new rectangle be denoted by newstart, newend. Then algorithm can be :-

for (i=n)
    if ( newstart < end[i] ) 
           { if ( newend > start [i] )
                  //overlapping so exit
             else
                  // not overlapping so continue with next line
                  i--
             }
     else
        {   // line being compared is finished bfore new one. so no over lapping. 
          Print line. 
          Break loop
        }

- Kaushik Lele October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Kaushik,
since we know the value of newstart and newend. Why do we need to do linear search. We can do binary search also and find two closest start and two closest end. just compare with the those 2 pairs.

- susheel October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this question should be answerd using computional geometry method. we can represnt each x-axis alingned rectangle by two points (x_left,y_top) (x_right,y_bottom) now the given rectangle can be represented by two ranges [a1,a2] [b1,b2]. lets say we have n rectangles each represented by two points as explained above. so we have on the screen 2n points.
now there is intersection with the given rectangle if and only if at least one of those points having x_point is in the range [a1,a2] and y_point in the range [b1,b2]. this problem can be solved using special data structure trees called RANGE TREES in this case 2 dimentional range tree in time (or comperison) complexity of O((logn)^2) where n is the number of points in our case there are 2n points so we have time complexity O((log2n)^2)

- dlev October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

fix : one should check if one of the four vertices(and not only two) of the ractangle lies inside the range of the given rectangle.

- dlev October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why don't u search for a point in a range with 2 operations ?

A<=x<=B instead of using a range tree ( which is nothing but a fancy name for a segment tree ) ? 4 operations per element And there are n elements , 4*n*n operations in total which is reducible to 4*n if the elements are sorted . Sorting again makes about O(n log n) comparisons . Thus , total comparisions are O( 4*n log n + 4*n) . This I suppose is the minimum .

And we are searching for a range which could be as big as min and max. The range tree or the segment tree will be a complete tree with O(max-min +1) nodes at the lower most level . Thus, the height of the tree will be O( log (max - min +1) ).

- sankalp October 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

when there are n elements and 4 operations per element, there itself you have the complexity of 4*n, how did you get 4*n*n?
Moreover if you do the brute force way, comparing all the rectangles the complexity would be O(n), why would anyone go for anything more than that?
I assume range tree helps finding elements between a range in O(logn). If we use 2D range tree how is it helping over 1D range tree? Because once we find range of x we don't need range of y, if we also look for range of y we end up with points in the rectangle

- Naveen Reddy Mandadi October 30, 2012 | 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