Amazon Interview Question






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

use O(nlogn) time to sort those rectangles by an axis (say, X), and then:
use O(n) time to find the set of intersected rectangles along the axis.

Use O(nlogn) time to sort by another axis(now it is Y), and then the same, use O(n) time to find the sec of intersected rectangles along Y axis.

Finally, use O(n) time to find the intersection of the two sets.

- Another Helper April 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This method is wrong consider test case like
(1). 0,0->10,10
(2). 1,11->4,12
(3). 5,5->15,15
(4). 11,1->13,4

where a->b denotes lower_left,upper_right co-ordinate of any rectangles.
Here rectangle (1) & (3) only intersects but you will not be able to catch it with your algorithm otherwise if i am wrong please let me know how your algorithm catches it.

I really don't think it's possible to solve it in O(NlogN) unless there is very complicated algorithm.

- Guest June 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

its a comp geometric problem....see line intersection algos

- utscool April 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anyone please let me know how the input is given here?
I mean, are co-ordinates of the rectangle given or a two dimensional array indicating vertices of the rectangle is given???

- Anon April 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Occasionally problems ask us to find the intersection of rectangles. There are a number of ways to represent a rectangle. For the standard Cartesian plane, a common method is to store the coordinates of the bottom-left and top-right corners.

Suppose we have two rectangles R1 and R2. Let (x1, y1) be the location of the bottom-left corner of R1 and (x2, y2) be the location of its top-right corner. Similarly, let (x3, y3) and (x4, y4) be the respective corner locations for R2. The intersection of R1 and R2 will be a rectangle R3 whose bottom-left corner is at (max(x1, x3), max(y1, y3)) and top-right corner at (min(x2, x4), min(y2, y4)). If max(x1, x3) > min(x2, x4) or max(y1, y3) > min(y2, y4) then R3 does not exist, ie R1 and R2 do not intersect.

source:

www[dot]topcoder[dot]com/tc?module=Static&d1=tutorials&d2=math_for_topcoders

- utscool April 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not right. how about one rectangular is tilted? Your representations of rectangular is even inaccurate.

- Ming April 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How will a rectangle be tilted in a plane?

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

if i find mid points of x and y axis of every rectangle..

sort them

check if every 2 consecutive rectangle intersect!!

- Anonymous April 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This Problem can be solved by representing the all the rectangle co-ordinates in the form of "Augmenting Height Balanced Trees" and finding the intersections by moving a sweep line across.

- k.krishnakarthik July 21, 2010 | 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