Skip to main content
Question Protected by 200_success
deleted 83 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Overlapping rectangles: Interview Question

GotI received the following question in a technical interview today (for a devops/SRE position): Write a function which returns true if the two rectangles passed to it as arguments would overlap if drawn on a cartesian plane. The rectangles are guaranteed to be aligned with the axes, not arbitrarily rotated.

Write a function which returns true if the two rectangles passed to it as arguments would overlap if drawn on a Cartesian plane. The rectangles are guaranteed to be aligned with the axes, not arbitrarily rotated.

    #/usr/bin/env python
    class Point(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y

    class Rect(object):
        def __init__(self, p1, p2):
            '''Store the top, bottom, left and right values for points 
               p1 and p2 are the (corners) in either order
            '''
            self.left   = min(p1.x, p2.x)
            self.right  = max(p1.x, p2.x)
            self.bottom = min(p1.y, p2.y)
            self.top    = max(p1.y, p2.y)
    #/usr/bin/env python 

    def overlap(r1,r2):
        '''Overlapping rectangles overlap both horizontally & vertically
        '''
        hoverlaps = True
        voverlaps = True
        if (r1.left > r2.right) or (r1.right < r2.left):
            hoverlaps = False
        if (r1.top < r2.bottom) or (r1.bottom > r2.top):
            voverlaps = False
        return hoverlaps and voverlaps

This seems to work (for all my test cases) but it also looks wrong to me. My My initial attempt was to start with hoverlaps and voverlaps as False, and selectively set them to True using conditions similar to these shown (but with the inequality operators reversed).

Overlapping rectangles: Interview Question

Got the following question in a technical interview today (for a devops/SRE position): Write a function which returns true if the two rectangles passed to it as arguments would overlap if drawn on a cartesian plane. The rectangles are guaranteed to be aligned with the axes, not arbitrarily rotated.

    #/usr/bin/env python
    class Point(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y

    class Rect(object):
        def __init__(self, p1, p2):
            '''Store the top, bottom, left and right values for points 
               p1 and p2 are the (corners) in either order
            '''
            self.left   = min(p1.x, p2.x)
            self.right  = max(p1.x, p2.x)
            self.bottom = min(p1.y, p2.y)
            self.top    = max(p1.y, p2.y)
    #/usr/bin/env python 

    def overlap(r1,r2):
        '''Overlapping rectangles overlap both horizontally & vertically
        '''
        hoverlaps = True
        voverlaps = True
        if (r1.left > r2.right) or (r1.right < r2.left):
            hoverlaps = False
        if (r1.top < r2.bottom) or (r1.bottom > r2.top):
            voverlaps = False
        return hoverlaps and voverlaps

This seems to work (for all my test cases) but it also looks wrong to me. My initial attempt was to start with hoverlaps and voverlaps as False, and selectively set them to True using conditions similar to these shown (but with the inequality operators reversed).

Overlapping rectangles

I received the following question in a technical interview today (for a devops/SRE position):

Write a function which returns true if the two rectangles passed to it as arguments would overlap if drawn on a Cartesian plane. The rectangles are guaranteed to be aligned with the axes, not arbitrarily rotated.

#/usr/bin/env python
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Rect(object):
    def __init__(self, p1, p2):
        '''Store the top, bottom, left and right values for points 
               p1 and p2 are the (corners) in either order
        '''
        self.left   = min(p1.x, p2.x)
        self.right  = max(p1.x, p2.x)
        self.bottom = min(p1.y, p2.y)
        self.top    = max(p1.y, p2.y)
#/usr/bin/env python 

def overlap(r1,r2):
    '''Overlapping rectangles overlap both horizontally & vertically
    '''
    hoverlaps = True
    voverlaps = True
    if (r1.left > r2.right) or (r1.right < r2.left):
        hoverlaps = False
    if (r1.top < r2.bottom) or (r1.bottom > r2.top):
        voverlaps = False
    return hoverlaps and voverlaps

This seems to work (for all my test cases) but it also looks wrong to me. My initial attempt was to start with hoverlaps and voverlaps as False, and selectively set them to True using conditions similar to these shown (but with the inequality operators reversed).

Notice removed Draw attention by CommunityBot
Bounty Ended with Peter Taylor's answer chosen by CommunityBot
Added python tag
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Tweeted twitter.com/#!/StackCodeReview/status/380621475042295808
Notice added Draw attention by Jim Dennis
Bounty Started worth 50 reputation by Jim Dennis
Added computational-geometry and collision tags. Added clarification that shapes are aligned to the axes.
Source Link

Got the following question in a technical interview today (for a devops/SRE position): Write a function which returns true if the two rectangles passed to it as arguments would overlap if drawn on a cartesian plane. The rectangles are guaranteed to be aligned with the axes, not arbitrarily rotated.

Got the following question in a technical interview today (for a devops/SRE position): Write a function which returns true if the two rectangles passed to it as arguments would overlap if drawn on a cartesian plane.

Got the following question in a technical interview today (for a devops/SRE position): Write a function which returns true if the two rectangles passed to it as arguments would overlap if drawn on a cartesian plane. The rectangles are guaranteed to be aligned with the axes, not arbitrarily rotated.

Source Link
Jim Dennis
  • 393
  • 1
  • 3
  • 8
Loading