Interview Questions

  • View all interview questions
  • blank
  • Step-by-step solution for step counting using recursion
    A lecturer at Hack Reactor made this comment on Quora in response to a question about preparing for the Hack Reactor admission's challenge:
    One big thing that will help you: get intimately familiar with the concepts of recursion and JavaScript closures ... they will come up early in your interactions with Hack Reactor (and are just great things to understand in general).
    Below we'll cover a clever question that we'll solve using recursion. In a previous post we covered some questions regarding closures.

    Sources

    https://www.quora.com/How-did-you-prepare-for-Hack-Reactors-admissions-challenge https://www.glassdoor.com/Interview/You-are-climbing-a-stair-case-Each-time-you-can-either-make-1-step-or-2-steps-The-staircase...


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

    mrdaniel published this on 12/6/15 | javascript, bootcamp, recursion, Fullstack Academy, Hack Reactor, Google
    Comments
  • +
  • 2
  • -
  • So when I was thinking about the problem, looking at 3 and 4 steps.. I was also able to arrive at Num_of_Ways = (n-1) + (n-2) This doesn't work when you arrive at 5 steps as it this equation would give 7 ways instead of 8. So how do you know to put the equation into recursion vs just having the above equation? Thank you!
  • +
  • 0
  • -
  • Wanted to post my solution as well. Adapted from the recursion example found in Eloquent JavaScript, Chapter 3: http://eloquentjavascript.net/
    function stairCounter(stairs){
    	var count = 0;
    	var start = 0;
    	function find(start){
    		if(start === stairs){
    			count += 1
    		}
    		else if(start > stairs)
    			return null
    		else{
    			return find(start + 2) || find(start + 1)
    		}
    	}
    	find(start);
    	return count;
    }
  • +
  • 0
  • -
  • Hope you like another way to solve it (Python):
    def steps(N):
        lst=[0,1,2]
        for i in range (3,N+1):
           sum=lst[i-1]+lst[i-2]
           lst.append(sum)
        return lst[N]
    
    Login to submit a comment