Wednesday, September 21, 2016

A failed phone interview

On the second day of my previous post, I kind of rushed into a phone interview by G. I didn’t do a good job because I wasted quite a bunch of time on the format of inputs. I should better communicate with the interviewer.
Question: given 2 rectangles, output the intersection rectangle.
def getIntersection(r1,r2):
    '''
    :type r1:list[float]
    :type r2:list[float]
    # only need 2 x-coordiantes and 2 y-coordinates to represent a rectangle. e.g.[x1,x2,y1,y2] #re max,min of x; max,min of y
    :rtype list[float]
    '''
    if r1[0]<=r2[1] or r1[1]>=r2[0] or\
        r1[2]<=r2[3] or r1[3]>=r2[2]:
        return []
    listx=r1[0:2]+r2[0:2]
    ans_x=[x for x in listx if x<max(listx) and x>min(listx)]
    listy=r1[2:]+r2[2:]
    ans_y=[x for x in listy if x<max(listy) and x>min(listy)]
    return ans_x+ans_y

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.