Doubling the Power of a Spell - codepath/compsci_guides GitHub Wiki

TIP102 Unit 7 Session 1 Standard (Click for link to problem statements)

Problem 5: Doubling the Power of a Spell

The court magician is practicing a spell that doubles its power with each incantation. Given an integer initial_power and a non-negative integer n, write a recursive function that doubles initial_power n times.

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10-15 mins
  • 🛠️ Topics: Recursion, Mathematical Operations

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • Q: What is the main task in this problem?
    • A: The task is to recursively double an initial power n times.
  • Q: What should the function return if n is 0?
    • A: The function should return the initial_power without any changes.
HAPPY CASE
Input: initial_power = 5, n = 3
Output: 40
Explanation: 5 doubled 3 times: 5 -> 10 -> 20 -> 40

Input: initial_power = 7, n = 2
Output: 28
Explanation: 7 doubled 2 times: 7 -> 14 -> 28

EDGE CASE
Input: initial_power = 10, n = 0
Output: 10
Explanation: If no doubling is required, the initial power remains 10.

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For Exponentiation via Doubling, we want to consider the following approaches:

  • Recursive Doubling: Recursively double the power by multiplying the current power by 2 and decrementing the count n until n reaches 0.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea:

  • To double the power n times, multiply the initial_power by 2 and call the function recursively with n-1.

Recursive Approach:

1) Base case: If `n` is 0, return the `initial_power` as no further doubling is needed.
2) Recursive case: 
   a) Multiply the `initial_power` by 2.
   b) Return the result of the function called with the updated `initial_power` and `n-1`.

⚠️ Common Mistakes

  • Not handling the base case correctly, which can result in incorrect results or infinite recursion.
  • Incorrectly updating the value of initial_power in each recursive call.

4: I-mplement

Implement the code to solve the algorithm.

def double_power(initial_power, n):
    # Base case: No more doubling needed
    if n == 0:
        return initial_power
    
    # Recursive case: Double the power and reduce n
    return double_power(initial_power * 2, n - 1)

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Trace through the double_power function with the input (5, 3). The function should return 40 after doubling 5 three times.
  • Test the function with edge cases like n = 0. The function should return the initial_power unchanged.

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(N) where N is the number of times the power needs to be doubled. The function performs N recursive calls.
  • Space Complexity: O(N) due to the recursion stack. The depth of recursion is proportional to n.