Interview Questions

  • View all interview questions
  • blank
  • Lucky sevens
    A user who was applying to App Academy posted this question on Coderbyte:
    Write a function called lucky_sevens which takes an array of integers and returns true if any three consecutive elements sum to 7.
    To solve this challenge we'll simply loop through the array starting at the 3rd position, and checking if the number at this index plus the two previous elements sums to 7. We continue doing this as we loop through the entire array. Once we find three elements that sum to 7, we simply return true. If we reach the end of the array without finding elements that sum to 7, we return false. Below is some code in JavaScript and Python with step-by-step comments.

    Sources

    https://coderbyte.com/question/app-academy-practice-questions-help http://prepwork.appacademy.io/coding-test-1/practice-coding-exercise/

    Code

    function lucky_sevens(arr) {
      
      // if less than 3 elements then this challenge is not possible
      if (arr.length < 3) {
        return "not possible";
      }
      
      // because we know there are at least 3 elements we can
      // start the loop at the 3rd element in the array (i=2)
      // and check it along with the two previous elements (i-1) and (i-2)
      for (var i = 2; i < arr.length; i++) {
        if (arr[i] + arr[i-1] + arr[i-2] === 7) {
          return true; 
        }
      }
      
      // if loop is finished and no elements summed to 7
      return false;
      
    }
    
    lucky_sevens([2, 1, 5, 1, 0]);
    
    def lucky_sevens(arr):
      
      # if less than 3 elements then this challenge is not possible
      if len(arr) < 3:
        return "not possible"
      
      # because we know there are at least 3 elements we can
      # start the loop at the 3rd element in the array (i=2)
      # and check it along with the two previous elements (i-1) and (i-2)
      for i in range(2, len(arr)):
        if (arr[i] + arr[i-1] + arr[i-2] == 7):
          return True
        
      # if loop is finished and no elements summed to 7
      return False
    
    print lucky_sevens([2, 1, 5, 1, 0])
    
    Run Code
    Run Code
    mrdaniel published this on 12/4/15 | javascript, bootcamp, array, App Academy, Fullstack Academy, Hack Reactor
    Comments
  • +
  • 6
  • -
  • don't listen to Anhtaihuman... we're not talking about whether three consecutive integers are 7, we are talking about whether they sum to 7. Your function should have returned true since 1 + 5 + 1 === 7
  • +
  • 0
  • -
  • To display code include the following tags:
    function luckySevens(arr){
      for (let i = 0; i< arr.length; i++){
        if (arr.slice(i,i+2).reduce((a,c)=>a+c) === 7) return true
        else if (arr.slice(i,i+3).reduce((a,c)=>a+c) === 7) return true
        }
      return false
    }
  • +
  • 0
  • -
  • nvm i got it
  • +
  • 0
  • -
  • I'm running the same code as mrdaniel and everything but i get false when calling 1,5,1 even with your code. What am i doing wrong? This is on Atom.
  • +
  • -15
  • -
  • A simpler way to do this would be to use RegEx.
    function lucky_sevens(arr) {
      return /777/g.test(arr.join(''));
    }
    
    lucky_sevens([2, 1, 5, 1, 0, 7, 7, 0, 7]);
    Login to submit a comment