Paypal SDE 2 Coding Interview: Climbing Stairs

Solve the popular "Climbing Stairs" coding interview question from PayPal using dynamic programming. Learn the iterative approach with Python code, time/space complexity analysis.

Mentor

Blog

Dynamic programming questions are popular in coding interviews, especially at companies like PayPal that deal with complex logistics and optimisation problems.

The "Climbing Stairs" problem is a classic DP question that tests your ability to break down a problem into smaller subproblems and reuse solutions efficiently.

Problem Statement:

Count Ways To Reach The N-th Stairs.

You have been given a number of stairs. Initially, you are at the 0th stair, and you need to reach the Nth stair.

Each time you can either climb one step or two steps. You are supposed to return the number of distinct ways in which you can climb from the 0th step to Nth step.

Solution:

To approach this dynamic programming problem, you can use the concept of recursion with memoization or iterative dynamic programming.

Concept: The number of ways to reach the N-th stair is the sum of the ways to reach the (N-1)-th stair and the (N-2)-th stair, because from the (N-1)-th stair, you can take a single step to reach the N-th stair, and from the (N-2)-th stair, you can take a two-step jump to reach the N-th stair.

Approach:

  1. Define a function countWays(n) that returns the number of ways to reach the N-th stair.
    1. Use a base case: if n is 0 or 1, there is only one way to be on the 0th or 1st stair.
      1. Recursively call countWays(n-1) + countWays(n-2) and store the results to avoid recomputation (memoization).
        1. Alternatively, use an iterative approach with a DP array where dp[i] represents the number of ways to reach the i-th stair.

          Here's a Python code snippet using iterative dynamic programming to solve the problem:

          def countWays(n):
              if n <= 1:
                  return 1
              dp = [0] * (n + 1)
              dp[0], dp[1] = 1, 1
              for i in range(2, n + 1):
                  dp[i] = dp[i - 1] + dp[i - 2]
              return dp[n]
          
          # Example usage:
          print(countWays(4))  # Output: 5
          

          Constraints and Assumptions:

          • The stairs are numbered from 0 to N.
            • The function should handle large values of N efficiently.
              • The result should fit within the standard data type limits (e.g., 32-bit integer).

                Time Complexity: O(n) - We compute the number of ways for each step once.

                Space Complexity: O(n) - We store the number of ways for each step up to N.

                This code calculates the number of distinct ways to climb to the N-th stair by iteratively building up the solution.

                ******


                That's how you can solve the classic "Climbing Stairs" dynamic programming problem asked in coding interviews at PayPal.

                If you're preparing for PayPal or other product-based company interviews, I'd be happy to guide you through more such questions and help you strengthen your problem-solving skills.

                Let’s connect and practice together! The more you practice, the more confident you'll be in your next interview.

                More technical interview questions and their solutions:

                Mastering the Binary Tree LCA Problem for Amazon SDE Interviews

                Find the Unique Number in Duplicates Array: Meta SDE Interview Question