-
Notifications
You must be signed in to change notification settings - Fork 244
Strongest Avenger
TIP102 Unit 7 Session 1 Standard (Click for link to problem statements)
The Avengers need to determine who is the strongest. Given a list of their strengths, find the maximum strength using a recursive approach without using the max
function.
- 💡 Difficulty: Medium
- ⏰ Time to complete: 15-20 mins
- 🛠️ Topics: Recursion, Finding Maximum
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 find the maximum strength value from a list of strengths using recursion.
- Q: Can we use built-in functions like
max
?- A: No, the problem explicitly asks to find the maximum using a recursive approach.
HAPPY CASE
Input: [88, 92, 95, 99, 97, 100, 94]
Output: 100
Explanation: The maximum strength among the Avengers is 100.
Input: [50, 75, 85, 60, 90]
Output: 90
Explanation: The maximum strength among the Avengers is 90.
EDGE CASE
Input: [10]
Output: 10
Explanation: There is only one strength value in the list, so the maximum is 10.
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 Finding Maximum Problems, we want to consider the following approaches:
- Recursive Search: Compare the first element with the maximum of the rest of the list and return the larger of the two.
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
- Use recursion to find the maximum value by comparing the first element with the maximum of the rest of the list.
Recursive Approach:
1) Base case: If the list `strengths` has only one element, return that element as the maximum.
2) Recursive case:
a) Call the function recursively on the rest of the list `strengths[1:]`.
b) Compare the first element `strengths[0]` with the maximum of the rest of the list.
c) Return the larger value.
- Forgetting the base case which can lead to infinite recursion.
- Incorrectly handling list slicing, which can result in errors.
Implement the code to solve the algorithm.
def strongest_avenger(strengths):
if len(strengths) == 1:
return strengths[0]
else:
max_of_rest = strongest_avenger(strengths[1:])
return strengths[0] if strengths[0] > max_of_rest else max_of_rest
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through the
strongest_avenger
function with the input[88, 92, 95, 99, 97, 100, 94]
. The function should return 100 after recursively comparing each element. - Trace through the function with the input
[50, 75, 85, 60, 90]
. The function should return 90 after recursively comparing each element.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
-
Time Complexity:
O(N)
whereN
is the number of elements in the list. The function makes one recursive call per element, leading to linear time complexity. -
Space Complexity:
O(N)
due to the recursion stack. The depth of recursion is proportional to the number of elements in the list, leading to linear space usage.