Bird
Raised Fist0
Interview Prepdp-linear-fibonaccieasyAmazonGoogleApple

Climbing Stairs

Choose your preparation mode4 modes available
▶
Steps
setup

Check if n <= 1

The algorithm first checks if the input n is less than or equal to 1. If so, it returns 1 immediately because there is only one way to climb zero or one step.

💡 This base case handles trivial inputs quickly and sets the foundation for the iterative approach.
Line:if n <= 1: return 1
💡 Recognizing base cases prevents unnecessary computation and clarifies the minimal problem size.
📊
Climbing Stairs - Watch the Algorithm Execute, Step by Step
Watching the algorithm step through each iteration reveals how the problem reduces to a Fibonacci-like sequence, making the logic clear without needing to parse code alone.
Step 1/10
·Active fill★Answer cell
Item 0 - wt:0 val:0
i\w0123
i=0????
Item 0 - wt:0 val:0
i\w0123
i=011??
ways(0)=1
Item 0 - wt:0 val:0
i\w0123
i=0112?
current = ways(0)+ways(1)
Item 0 - wt:0 val:0
i\w0123
i=0112?
first = ways(1)
Item 0 - wt:0 val:0
i\w0123
i=01123
current = ways(1)+ways(2)
Item 0 - wt:0 val:0
i\w0123
i=01123
first = ways(2)
Item 0 - wt:0 val:3
i\w0123
i=01123
Answer: ways(3)
Item 0 - wt:0 val:3
i\w0123
i=01123
ways(0)=1
Item 0 - wt:0 val:3
i\w0123
i=01123
first
Item 0 - wt:0 val:3
i\w0123
i=01123
Final Answer

Key Takeaways

✓ The problem reduces to a Fibonacci sequence where each step's ways depend on the sum of the previous two steps.

This insight is hard to see from code alone because the recurrence is implicit; watching the values evolve makes it explicit.

✓ Only two variables are needed to compute the answer, demonstrating space optimization in dynamic programming.

Seeing the sliding window update clarifies how DP can be done without a full table.

✓ The base cases (steps 0 and 1) are critical and set the foundation for the entire computation.

Understanding base cases is essential because they anchor the recurrence and prevent errors.

Practice

(1/5)
1. You are given an array representing daily stock prices. You want to find the maximum profit possible by buying once and selling once after the buy day. Which approach guarantees the optimal solution with linear time and constant space complexity?
easy
A. Use nested loops to check all pairs of buy and sell days and track the maximum profit.
B. Use a dynamic programming table storing maximum profit for each day and each possible transaction count.
C. Iterate once, tracking the minimum price so far and updating the maximum profit by comparing current price minus minimum price.
D. Apply a greedy algorithm that always sells on the next day if the price is higher than the buy day.

Solution

  1. Step 1: Understand problem constraints

    The problem requires exactly one buy and one sell, with the sell day after the buy day, maximizing profit.
  2. Step 2: Identify optimal approach

    Tracking the minimum price so far and updating max profit in one pass ensures O(n) time and O(1) space, guaranteeing optimality.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    One pass with min price tracking is classic and efficient [OK]
Hint: Track min price and max profit in one pass [OK]
Common Mistakes:
  • Using nested loops causes TLE on large inputs
  • Greedy selling next day misses better future profits
  • DP table wastes space for this simple case
2. You are given a list of daily stock prices and want to maximize profit by buying and selling any number of times, but you cannot hold more than one stock at a time. Which algorithmic approach guarantees the optimal solution for this problem?
easy
A. Greedy approach summing all positive price differences between consecutive days
B. Dynamic Programming using a state machine to track holding and not holding states
C. Pure brute force recursion exploring all buy/sell combinations without memoization
D. Divide and conquer by splitting the price array and merging results

Solution

  1. Step 1: Understand problem constraints

    The problem allows unlimited transactions but only one stock can be held at a time.
  2. Step 2: Identify suitable algorithm

    For this problem, the greedy approach of summing all positive price differences between consecutive days guarantees the optimal profit. This works because any profitable transaction can be decomposed into these increments.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Greedy summing positive differences is optimal for unlimited transactions without cooldown or fees [OK]
Hint: Sum all positive price differences between consecutive days to get max profit [OK]
Common Mistakes:
  • Assuming DP is required when greedy suffices
  • Using brute force without memoization
  • Trying divide and conquer without state tracking
3. What is the time complexity of the space-optimized DP solution for the Best Time -- 2 Transactions problem with input size n and fixed k=2?
medium
A. O(n^2)
B. O(n * k)
C. O(n)
D. O(k^n)

Solution

  1. Step 1: Identify loops in the code

    Outer loop runs n-1 times, inner loop runs k times (k=2 fixed). So total iterations ~ n*k.
  2. Step 2: Simplify complexity with fixed k

    Since k=2 is constant, O(n*k) simplifies to O(n).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Linear time due to fixed number of transactions [OK]
Hint: Fixed k means linear time, not quadratic [OK]
Common Mistakes:
  • Confusing k as variable leading to O(n*k)
  • Assuming nested loops always mean O(n^2)
4. If you are allowed to complete as many transactions as you like (buy and sell multiple times), which modification to the algorithm below correctly computes the maximum profit?
hard
A. Sum up all positive differences between consecutive days' prices to accumulate profit from every profitable transaction.
B. Keep the same code but multiply the final max_profit by the number of transactions allowed.
C. Use the same min_price tracking but reset min_price to current price after each profitable sale.
D. Use nested loops to check all pairs of buy and sell days and pick the maximum sum of profits from multiple transactions.

Solution

  1. Step 1: Understand multiple transactions scenario

    When unlimited transactions are allowed, profit is sum of all positive price differences between consecutive days.
  2. Step 2: Identify correct algorithm modification

    Summing positive differences directly captures all profitable trades without complex state tracking.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Summing positive daily gains yields max profit with unlimited transactions [OK]
Hint: Sum all positive consecutive price differences [OK]
Common Mistakes:
  • Multiplying single transaction profit by transaction count
  • Resetting min_price incorrectly
  • Using nested loops causing inefficiency
5. Suppose the Jump Game problem is modified so that you can reuse positions multiple times (i.e., cycles allowed). Which approach correctly handles this variant to determine if the last index is reachable?
hard
A. Use the greedy approach as is, since it already tracks max reachable index
B. Use DFS with memoization to detect cycles and avoid infinite loops
C. Use bottom-up DP iterating forwards to accumulate reachable states
D. Use a simple BFS traversal marking visited indices to detect reachability

Solution

  1. Step 1: Understand reuse implies cycles

    Allowing reuse means cycles can occur, so greedy approach may fail as it assumes forward progress.
  2. Step 2: Identify correct approach

    BFS with visited marking detects cycles and explores reachable indices without infinite loops.
  3. Step 3: Why other options fail

    DFS with memoization can work but is less efficient and more complex; greedy fails; bottom-up DP forwards is incorrect due to dependency issues.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    BFS handles cycles and reachability correctly [OK]
Hint: Cycles require graph traversal with visited checks [OK]
Common Mistakes:
  • Assuming greedy works with cycles
  • Using DFS without cycle detection
  • Using bottom-up DP forwards incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard