HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Counting the overlapping intervals in the union of two sets

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thecountingunionintervalsoverlappingtwosets

Problem

I had to recently implement a code to count the union of all intervals (if there are overlapping intervals) for an interview. I was provided with the following function stub:

public int solution(int[] A, int[] B) {

}


A[0] and B[0] form one interval, A[1] and B[1] the next one...and so on.

I tried two approaches.

-
Copying all the intervals to an object and using collections to sort it based on the first value of an interval(Array A[]). But if the number of intervals is large, then is it a good idea to do so?

```
public class Solution {

class Pair {
private int a,b;
Pair() {}
Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public int getB() {
return b;
}
}

class PairListComparator implements Comparator {

@Override
public int compare(Pair o1, Pair o2) {
if(o1.a o2.a)
return 1;
else {
if(o1.b o2.b)
return 1;
else return 0;
}
}

}

ArrayList inputList = new ArrayList();

Solution() {

}

public void createInputList(int[] A, int[] B) {
for(int i = 0; i secondB ? firstB : secondB;
inputList.set(ptr, new Pair(firstA, newB));
ptr++;
}
else {
count++;
}
}
return count;
}

public int solution(int[] A, int[] B) {
// write your code in Java SE 8
if((A.length != B.length) || (A.length == 0))
return 0;
createInputList(A, B);
return evaluateList();
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Solution s = new Solution();
/*
* [1, 12, 42, 70, 36, -4, 43, 15], [5, 15, 44, 72, 36, 2, 69, 24]
*/

Solution

Bugs

I tried this test input:

int[] a = {1, 10, 30, 60 };
    int[] b = {2, 20, 40, 70 };


and your program returned 3. Shouldn't the answer be 4 because there are 4 distinct intervals?

Then I tried this test input:

int[] a = {1,   10, 30, 60 };
    int[] b = {100, 20, 40, 70 };


and your program returned 2. I would have expected the answer to be 1 because the first interval is a superset of all the other intervals.
Corrected code

Your second implementation seemed to be closer to working than the first. I made the following adjustments to your second implementation to fix the above problems:

public int evaluateList(int[] A, int[] B) {
    int count = 1;
    int maxB  = B[0];

    for (int i = 1; i  B[i] ? maxB : B[i];
        } else {
            count++;
            maxB = B[i];
        }
    }
    return count;
}

Code Snippets

int[] a = {1, 10, 30, 60 };
    int[] b = {2, 20, 40, 70 };
int[] a = {1,   10, 30, 60 };
    int[] b = {100, 20, 40, 70 };
public int evaluateList(int[] A, int[] B) {
    int count = 1;
    int maxB  = B[0];

    for (int i = 1; i < A.length; i++) {
        if (A[i] <= maxB) {
            maxB = maxB > B[i] ? maxB : B[i];
        } else {
            count++;
            maxB = B[i];
        }
    }
    return count;
}

Context

StackExchange Code Review Q#113655, answer score: 5

Revisions (0)

No revisions yet.