Interview Questions

  • View all interview questions
  • blank
  • 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.



    You need to be a premium member to see the rest of this question and code.

    Become a Premium Member

    With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

    “If you're able to solve Medium Coderbyte problems and have a good understanding of web development basics [...] then you are probably ready for admissions at the top schools.” ― Huntly Mayo-Malasky
    • 200+ Coding Challenges
    • Mock Interview Questions
    • 500,000+ Code Solutions
    • Algorithm Tutorials
    • Interview Prep Courses
    mrdaniel published this on 12/25/15 | javascript, bootcamp, array, Fullstack Academy
    Comments
  • +
  • 2
  • -
  • To display code include the following tags:
    
    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(arr[i] === arr[j]){
            overlapNums.push(arr[i]);
            if(overlapNums.length === lastEl){
              return true;
            }
          }
        }
      }
      return false;
    }
    
    
  • +
  • 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.
  • +
  • 0
  • -
  • 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)
    
  • +
  • 0
  • -
  • 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] )
  • +
  • -1
  • -
  • To display code include the following tags:
    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;
    }
    
    Login to submit a comment