Facebook Interview Question
SDE1sCountry: United States
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++;
}
}
}
- mr.robot.fun.society November 04, 2017