Facebook Interview Question for SDE1s


Country: United States




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

#include<iostream>
#include<utility>
#include<vector>

using namespace std;

bool isOverlap( const pair<int,int>& p1,
                const pair<int,int>& p2 )
{
	if( p1 == p2 )
	{
		return true;
	}
	else if( p1 < p2 )
	{
		return ( p1.second >= p2.first ) ? true : false;
	}
	else
	{
		return ( p2.second >= p1.first ) ? true :false;
	}	
}

vector< pair<int,int>> getRange( const vector< pair<int,int> >& arr1,
                                 const vector< pair<int,int> >& arr2)
{
	vector< pair<int,int> > result;
	
	// get intersection of timestamps
	size_t i=0;
	size_t j=0;
	
	while( i< arr1.size() && j<arr2.size() )
	{
		// If they dont overlap then advance smaller pair index
		if( !isOverlap( arr1[i], arr2[j]))
		{
			( arr1[i] < arr2[j] )? ++i : ++j;
			continue;
		}
		
		if( arr1[i] == arr2[j])
		{
			result.push_back( arr1[i]);
			++i;
			++j;
		}
		else 
		{
			int left = max( arr1[i].first, arr2[j].first );
			int right = min( arr1[i].second, arr2[j].second );
				
			result.push_back( make_pair( left, right));
			(right == arr1[i].second) ? ++i : ++j;
		}
	}
	
	return result;
}

int main()
{
	vector< pair<int,int> > ip1 = { {2,4}, {9,11}, {14,18}, {20,22}};
	vector< pair<int,int> > ip2 = { {3,5}, {9,10}, {15,17}, {22,25}};

	vector< pair<int,int> > result = getRange( ip1, ip2 );
	for( auto item : result )
	{
		cout << item.first << " : " << item.second << endl;
	}
	return 0;
}

- mr.robot.fun.society November 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean checkIfOverLap(int [] first, int [] second) {
        if (first[1] < second[0] || second[1] < first[0]) return false;
        return true;
    }

    public void findOverlaps(int [][] first, int [][] second) {
        int i=0;
        int j=0;

        while (i < first.length && j < second.length) {
            // check overlap
            if (checkIfOverLap(first[i], second[j])) {
                int start = Math.max(first[i][0], second[j][0]);
                int end = Math.min(first[i][1], second[j][1]);
                System.out.println("Interval intersect: " + start + " ," + end);
                if (first[i][1] < second[j][0]) i++;
                else j++;
            } else {
                // don't overlap
                if (first[i][1] < second[j][0]) i++;
                else j++;
            }
        }
    }

- Cooked March 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Python Solution:

l = []
for i in range(len(A)):
	if A[i][1] < B[i][0] or B[i][1] < A[i][0]:
		l.append((0,0))
	else:
		l.append((max(A[i][0],B[i][0]),min(A[i][1],B[i][1])))
		
print(l)

- praveenp November 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fail!
A= [(1, 5), (7, 11)]
B=[(1,2), (3,5), (9, 10)]

- sri December 14, 2017 | 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