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 <= 30When 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:
Imagine you want to find a specific number in a growing sequence where each new value is created by adding the previous two values together. The brute force approach simply builds the sequence from the very beginning, step by step, by constantly looking backward to add the last two numbers until you reach your target position.
Here's how the algorithm would work step-by-step:
def fibonacci_number(target_position):
# Handle edge cases where the target position is the initial baseline values
if target_position <= 1:
return target_position
sequence_list = [0, 1]
# Iteratively build the sequence forward until we reach the target position
for current_step in range(2, target_position + 1):
next_sequence_value = sequence_list[-1] + sequence_list[-2]
sequence_list.append(next_sequence_value)
# Return the final calculated value at the exact target position
return sequence_list[-1]Instead of recalculating previous values over and over again from scratch, the optimal approach builds the sequence from the ground up by remembering previous results. By storing the last two numbers we calculated, we can quickly figure out the next one without doing repetitive work.
Here's how the algorithm would work step-by-step:
def fib(target_position):
# Base cases handle the start of the sequence without looping.
if target_position == 0:
return 0
if target_position == 1:
return 1
previous_fibonacci = 0
current_fibonacci = 1
# Iteration computes values upwards to avoid redundant calculations.
for step_index in range(2, target_position + 1):
next_fibonacci = previous_fibonacci + current_fibonacci
previous_fibonacci = current_fibonacci
current_fibonacci = next_fibonacci
# Return the final accumulated result once the target is reached.
return current_fibonacci| Case | How to Handle |
|---|---|
| Input n is 0 | The base case check immediately returns 0 as defined by the Fibonacci sequence. |
| Input n is 1 | The base case check correctly identifies the input and returns 1. |
| Negative integer inputs | Throw an illegal argument exception since Fibonacci numbers are defined only for non-negative integers. |
| Large integer inputs exceeding standard 32-bit integer limits | Use 64-bit integers or BigInteger data types to prevent numeric overflow during computation. |
| Repeated recursive calls for overlapping subproblems in a naive implementation | Apply memoization or iterative bottom-up dynamic programming to reduce the time complexity to linear. |
| Deep recursion causing a stack overflow error for moderately large n | Prefer an iterative approach with O(1) space complexity to avoid call stack limits entirely. |