Interview Questions

Determine overlapping numbers in ranges

The question is as follows:
## Example

If the array is: [10, 20, 4, 14, 4] then the ranges are:
**true**.
## Code

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.

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 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

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.

gabewr

commented on 05/09/16

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]

xpfoup

commented on 10/16/18

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)

montekaka

commented on 05/10/17

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] )

gabrieldefazio

commented on 08/01/17

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; }

sparent

commented on 07/18/18

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])

rchap504

commented on 07/18/18

-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; }

bpaksoy

commented on 12/09/16

Log in to submit a comment.