-
Notifications
You must be signed in to change notification settings - Fork 242
Calculating Groot's Growth
TIP102 Unit 7 Session 1 Standard (Click for link to problem statements)
Groot grows according to a pattern similar to the Fibonacci sequence. Given n
, find the height of Groot after n
months using a recursive method.
The Fibonacci numbers, commonly denoted F(n)
, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0
and 1
. That is,
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
- 💡 Difficulty: Medium
- ⏰ Time to complete: 20 mins
- 🛠️ Topics: Recursion, Fibonacci Sequence
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 calculate the height of Groot after
n
months using the Fibonacci sequence.
- A: The task is to calculate the height of Groot after
- Q: Are there any special cases for small values of
n
?- A: Yes, the base cases are when
n = 0
andn = 1
.
- A: Yes, the base cases are when
HAPPY CASE
Input: 5
Output: 5
Explanation: The 5th Fibonacci number is 5.
Input: 8
Output: 21
Explanation: The 8th Fibonacci number is 21.
EDGE CASE
Input: 0
Output: 0
Explanation: The 0th Fibonacci number is 0.
Input: 1
Output: 1
Explanation: The 1st Fibonacci number is 1.
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 Recursive Fibonacci Problems, we want to consider the following approaches:
-
Recursive Calculation: Calculate the Fibonacci number by summing the two preceding numbers in the sequence with base cases for
n = 0
andn = 1
.
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
- Use a recursive function to calculate the Fibonacci number at position
n
. Define the base cases forn = 0
andn = 1
.
Recursive Approach:
1) Base case: If `n` is 0, return 0.
2) Base case: If `n` is 1, return 1.
3) Recursive case: Return the sum of `fibonacci_growth(n - 1)` and `fibonacci_growth(n - 2)`.
- Forgetting to define the base cases, which would lead to infinite recursion.
- Misunderstanding the Fibonacci sequence and incorrectly summing the terms.
Implement the code to solve the algorithm.
def fibonacci_growth(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci_growth(n - 1) + fibonacci_growth(n - 2)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through the
fibonacci_growth
function with the input5
. The function should return 5 after calculatingfibonacci_growth(4)
andfibonacci_growth(3)
recursively. - Trace through the function with the input
8
. The function should return 21 after recursively calculatingfibonacci_growth(7)
andfibonacci_growth(6)
.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
-
Time Complexity:
O(2^N)
whereN
is the inputn
. The function makes two recursive calls for each non-base case, leading to exponential growth in the number of calls. -
Space Complexity:
O(N)
due to the recursion stack. The depth of the recursion is proportional ton
, leading to linear space usage.