Interview Questions

Determine overlapping numbers in ranges
The question is as follows:
You will be given an array with 5 numbers. The first 2 numbers represent a range, and the next two numbers represent another range. The final number in the array is X. The goal of your program is to determine if both ranges overlap by at least X numbers. For example, in the array [4, 10, 2, 6, 3] the ranges 4 to 10 and 2 to 6 overlap by at least 3 numbers (4, 5, 6), so your program should return true.

Example

If the array is: [10, 20, 4, 14, 4] then the ranges are:
10 11 12 13 14 15 16 17 18 19 20 4 5 6 7 8 9 10 11 12 13 14
These ranges overlap by at least 4 numbers, namely: 10, 11, 12, 13, 14 so your program should return true.

Code

```function OverlappingRanges(arr) {

// keep a count of how many numbers overlap
var counter = 0;

// loop through one of the ranges
for (var i = arr[0]; i < arr[1]; i++) {

// check if a number within the first range exists
// in the second range
if (i >= arr[2] && i <= arr[3]) {
counter += 1;
}

}

// check if the numbers that overlap is equal to or greater
// than the last number in the array
return (counter >= arr[4]) ? true : false;
}

OverlappingRanges([4, 10, 2, 6, 3]);
```
```def OverlappingRanges(arr):

# keep a count of how many numbers overlap
counter = 0

# loop through one of the ranges
for i in range(arr[0], arr[1]):

# check if a number within the first range exists
# in the second range
if (i >= arr[2] and i <= arr[3]):
counter += 1

# check if the numbers that overlap is equal to or greater
# than the last number in the array
if counter >= arr[4]:
return 'true'
else:
return 'false'

print OverlappingRanges([4, 10, 2, 6, 3])
```
mrdaniel published this on 12/25/15 |
• +
• 2
• -
• This question should be able to answer without looping. For example: ```function overlapping(input){ var nums1 = listOfNums(input[0], input[1]); var nums2 = listOfNums(input[2], input[3]); var overlappingNum = 0; if(nums1[0] >= nums2[0] && nums1[0] <= nums2[1]){ overlappingNum = nums2[1] - nums1[0] + 1; } else { overlappingNum = nums1[1] - nums2[0] + 1; } if(overlappingNum >= input[4]){ return true; } } function listOfNums(a, b){ var start = a; var end = b; if(a > b){ start = b; end = a; } return [a, b]; } var a = [4, 10, 2, 6, 3]; overlapping(a) ```
• +
• 2
• -
• I would it this way: ``` def OverlappingRanges(arr): return len(range(max([arr[0], arr[2]]), min([arr[1], arr[3]]) + 1)) >= arr[4] ```
• +
• 1
• -
• Not sure what's going on, but when I test this code in repl.it it says that the code is taking too long to execute and to be careful of infinite loops.
• +
• 1
• -
• ```function rangeOfRanges(arr){ let arr1 = [], arr2 = [] for(let i= arr[0]; i <= arr[1]; i++){ arr1.push(i) } for(let i= arr[2]; i <= arr[3]; i++){ arr2.push(i) } return arr1.filter((number)=> arr2.indexOf(number)>-1).length >= arr[4] } rangeOfRanges([4, 8, 5, 6, 3] )```
• +
• 0
• -
• Since range() is not inclusive shouldn't the line be the following?
```for i in range(arr[0], arr[1] + 1):

```
• +
• 0
• -
• ```#include<stdio.h>
int main()
{
int a[5],b[100],e[100],i,j,d,c,k,count=0,flag=0;
printf("Enter elements of the array: ");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
c=a[1]-a[0]+1;
for(j=0;j<c;j++)
{
b[j]=a[0];
a[0]++;
}
d=a[3]-a[2]+1;
for(k=0;k<d;k++)
{
e[k]=a[2];
a[2]++;
}
for(i=0;i<c;i++)
{
for(j=0;j<d;j++)
{
if(b[i]==e[j])
{
count++;
flag=1;
}
}
}
if(flag==1)
{
printf("These ranges overlap by atleast %d numbersn",count);
printf("TRUE");
}
else
{
printf("FALSE");
}
return 0;
}
```
• +
• 0
• -
• Easiest way? ```def OverlappingRanges(arr): if (arr[3] - arr[0]) + 1 >= arr[4]: return "true" else: return "false" print OverlappingRanges([4, 10, 2, 6, 2])```
• +
• 0
• -
• ``` function OverlappingRanges(arr) { /* Only 2 conditions: */ /* 1- The fifth number should not be negative. */ /* 2- If the fifth number is 0 then it's always true. */ if(arr[4]<0) return false; else if(arr[4]==0) return true; var maxStart=Math.max(arr[0], arr[2]); var minEnd=Math.min(arr[1], arr[3]); if(minEnd>=maxStart){ if(minEnd-maxStart+1>=arr[4]) return true; else return false; } else return false; } ```
• +
• 0
• -
• ``` function OverlappingRanges(arr) { var min=Math.max(arr[0],arr[2]); var max=Math.min(arr[1],arr[3]); return (max-min)+1 >= arr[4]; } ```
• +
• -1
• -
• ``` function overlap(arr){ var overlapNums = []; var lastEl = arr.pop(); for(var i = arr[0]; i <= arr[1]; i++){ for(var j = arr[2]; j <= arr[3]; j++) { if(i === j){ overlapNums.push(i); if(overlapNums.length === lastEl){ return true; } } } } return false; } ```
• +
• -1
• -
• ```a=list(map(int,input("Enter elements of arrayn").split(' ')))
r1=[i for i in range(a[0],a[1]+1)]
r2=[i for i in range(a[2],a[3]+1)]
print(True) if len((set(r1)).intersection(set(r2)))>=a[4] else print(False)
```