Codepath

Collecting Infinity Stones

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

Problem 2: Collecting Infinity Stones

Thanos is collecting Infinity Stones. Given an array of integers stones representing the power of each stone, return the total power using a recursive approach.

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 15 mins
  • 🛠️ Topics: Recursion

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 sum the total power of all stones in the array using a recursive function.
  • Q: Can we use iteration?
    • A: No, the problem explicitly asks for a recursive solution.
HAPPY CASE
Input: [5, 10, 15, 20, 25, 30]
Output: 105
Explanation: The sum of the stones' power is 5 + 10 + 15 + 20 + 25 + 30 = 105.

Input: [12, 8, 22, 16, 10]
Output: 68
Explanation: The sum of the stones' power is 12 + 8 + 22 + 16 + 10 = 68.

EDGE CASE
Input: []
Output: 0
Explanation: The list is empty, so the sum should be 0.

Input: [100]
Output: 100
Explanation: The list has only one stone with power 100.

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 Summation Problems, we want to consider the following approaches:

  • Recursion: Break the problem down by summing the first element with the sum of the rest of the list.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea:

  • Use a recursive function where the base case is an empty list, and the recursive case adds the first element to the sum of the rest of the list.

Recursive Approach:

1) Base case: If the list `stones` is empty, return 0.
2) Recursive case: Return the first element of `stones` plus the result of a recursive call to the same function with the rest of the list (excluding the first element).

⚠️ Common Mistakes

  • Forgetting the base case can lead to infinite recursion.
  • Mismanaging the indices or sublist extraction can result in incorrect sums.

4: I-mplement

Implement the code to solve the algorithm.

def collect_infinity_stones(stones):
    if not stones:
        return 0
    return stones[0] + collect_infinity_stones(stones[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 collect_infinity_stones function with the input [5, 10, 15, 20, 25, 30]. The function should return 105 after six recursive calls.
  • Trace through the function with the input [12, 8, 22, 16, 10]. The function should return 68 after five recursive calls.

6: E-valuate

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

Assume N represents the number of stones in the list.

  • Time Complexity: O(N) where N is the number of stones in the list. This is because the function makes one recursive call per stone in the list, summing them up in linear time.
  • Space Complexity: O(N) due to the recursion stack. Each recursive call adds a new frame to the stack, leading to linear space usage proportional to the number of stones.
Fork me on GitHub