Facebook (SDE I) Phone Screen Montreal (How to answer this follow up?)
Anonymous User
786

Given a set of sessions, find the maximum number of overlapping sessions. As a follow up was asked to return the overlapped session.

I gave the following solution to return the maximum number of overlap among these intervals at any time. But couldn't answer the follow up to find the overlapped interval.

def overlap(v): 

    # variable to store the maximum 
    # count 
    ans = 0
    count = 0
    data = [] 

    # storing the x and y 
    # coordinates in data vector 
    for i in range(len(v)): 

        # pushing the x coordinate 
        data.append([v[i][0], 'x']) 

        # pushing the y coordinate 
        data.append([v[i][1], 'y']) 

    # sorting of ranges 
    data = sorted(data) 

    # Traverse the data vector to 
    # count number of overlaps 
    for i in range(len(data)): 

        # if x occur it means a new range 
        # is added so we increase count 
        if (data[i][1] == 'x'): 
            count += 1
			if ans < count:
				# updating the value of ans 
				# after every traversal 
				ans = max(ans, count) 

        # if y occur it means a range 
        # is ended so we decrease count 
        if (data[i][1] == 'y'): 
            count -= 1
			
    # return the maximum value 
    return ans

# Driver code 
v = [[ 1, 2 ], [ 2, 4 ], [ 3, 6 ],[3,8]] 
overlap(v) 

This returns 3. But couldn't answer the best way to also return the maximum overlapping interval. In this case which should be [3,4]. I was wondering how could I have modified my existing solution to derive the solution to this follow up in Python?

Comments (6)