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.
Given n
, calculate F(n)
.
Example 1:
Input: n = 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: n = 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3:
Input: n = 4 Output: 3 Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
Constraints:
0 <= n <= 30
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The Fibonacci sequence starts with 0 and 1. To find the nth Fibonacci number using a brute force approach, we'll calculate all the Fibonacci numbers leading up to it by repeatedly adding the previous two.
Here's how the algorithm would work step-by-step:
def fibonacci_brute_force(n):
if n <= 0:
return 0
if n == 1:
return 1
# Initialize the first two Fibonacci numbers.
previous_fibonacci = 0
current_fibonacci = 1
# Iterate to calculate the nth Fibonacci number.
for _ in range(2, n + 1):
# Calculate the next Fibonacci number
# from the sum of the previous two.
next_fibonacci = previous_fibonacci + current_fibonacci
# Update previous and current Fibonacci numbers.
# Necessary to move sequence forward
previous_fibonacci = current_fibonacci
current_fibonacci = next_fibonacci
return current_fibonacci
To efficiently find a specific Fibonacci number, we avoid recalculating previously computed values. We use a technique where we store and reuse results, allowing us to build up to the desired number directly.
Here's how the algorithm would work step-by-step:
def fibonacci_number(n):
if n <= 0:
return 0
if n == 1:
return 1
previous_fibonacci_number = 1
current_fibonacci_number = 1
# Iterate to build Fibonacci sequence
for _ in range(2, n):
next_fibonacci_number = previous_fibonacci_number + current_fibonacci_number
# Update values for next iteration
previous_fibonacci_number = current_fibonacci_number
# Store the next fib number
current_fibonacci_number = next_fibonacci_number
# Cache results for optimization
return current_fibonacci_number
Case | How to Handle |
---|---|
Negative input n | Define behavior; either throw an exception, return an error code, or define F(n) for negative n mathematically. |
n = 0 | Return 0 as per the definition of the Fibonacci sequence F(0) = 0. |
n = 1 | Return 1 as per the definition of the Fibonacci sequence F(1) = 1. |
Large n leading to potential integer overflow | Use a data type capable of holding large Fibonacci numbers (e.g., long or BigInteger) or throw an error if overflow is unavoidable. |
Very large n causing stack overflow in recursive implementations | Use an iterative approach or memoization with dynamic programming to avoid excessive recursion depth. |
n exceeding available memory for memoization (very large n) | Consider space-optimized dynamic programming (only storing the two previous Fibonacci numbers) or limit the input n. |
Input is not an integer (e.g., float, string) | Type-check input n and either cast to integer (if appropriate and safe) or throw an error if non-integer input is not allowed. |
n is a very large number but the required value is zero (Modulo arithmetic would cause this result). | Check for this condition and appropriately handle it based on specifications, potentially using approximation to find the correct answer |