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.

    mrdaniel published this on 12/25/15 | javascript, bootcamp, array, Fullstack Academy
    Comments
  • +
  • 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
  • -
  • 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;
    }
    
    
  • +
  • 0
  • -
  • 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