Write a generator function that returns a generator object which yields the fibonacci sequence.
The fibonacci sequence is defined by the relation Xn = Xn-1 + Xn-2.
The first few numbers of the series are 0, 1, 1, 2, 3, 5, 8, 13.
Example 1:
Input: callCount = 5 Output: [0,1,1,2,3] Explanation: const gen = fibGenerator(); gen.next().value; // 0 gen.next().value; // 1 gen.next().value; // 1 gen.next().value; // 2 gen.next().value; // 3
Example 2:
Input: callCount = 0 Output: [] Explanation: gen.next() is never called so nothing is outputted
Constraints:
0 <= callCount <= 50When 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. The brute force approach calculates each number in the sequence by directly using the definition: each number is the sum of the two preceding numbers.
Here's how the algorithm would work step-by-step:
def generate_fibonacci_sequence_brute_force(number_of_terms):
fibonacci_sequence = []
if number_of_terms <= 0:
return fibonacci_sequence
# First Fibonacci number is 0
fibonacci_sequence.append(0)
if number_of_terms == 1:
return fibonacci_sequence
# Second Fibonacci number is 1
fibonacci_sequence.append(1)
if number_of_terms == 2:
return fibonacci_sequence
# Need to calculate subsequent numbers based on previous two
for i in range(2, number_of_terms):
# Sum the prior two elements
next_fibonacci_number = fibonacci_sequence[i - 1] + fibonacci_sequence[i - 2]
# Add number to sequence
fibonacci_sequence.append(next_fibonacci_number)
return fibonacci_sequenceThe most efficient way to generate the Fibonacci sequence avoids recalculating values. Instead, we remember the last two numbers in the sequence to quickly compute the next one. This process builds the sequence one number at a time using values that were already computed.
Here's how the algorithm would work step-by-step:
def generate_fibonacci_sequence(number_of_terms):
fibonacci_sequence = []
if number_of_terms <= 0:
return fibonacci_sequence
previous_number = 0
current_number = 1
fibonacci_sequence.append(previous_number)
if number_of_terms == 1:
return fibonacci_sequence
fibonacci_sequence.append(current_number)
# Start from 2 since we already have the first two terms.
for i in range(2, number_of_terms):
# Calculate the next Fibonacci number.
next_number = previous_number + current_number
fibonacci_sequence.append(next_number)
# Update the last two numbers for the next iteration.
previous_number = current_number
current_number = next_number
return fibonacci_sequence| Case | How to Handle |
|---|---|
| Negative input n | Return an empty list or raise an IllegalArgumentException, as the Fibonacci sequence is typically defined for non-negative integers. |
| Zero input n | Return a list containing only [0] or an empty list based on specific requirements, as F(0) = 0. |
| Input n = 1 | Return a list containing [0, 1] or just [0] and [1], depending if starting from zero is desired. |
| Large input n leading to integer overflow | Use a data type with larger capacity like 'long' in Java/C++ or consider using arbitrary-precision arithmetic libraries to handle very large Fibonacci numbers accurately. |
| Extremely large n causing excessive memory allocation | Use iterative approach that stores only the last two Fibonacci numbers, avoiding storing the entire sequence in memory. |
| n is not an integer | Cast the input to an integer or throw IllegalArgumentException if the algorithm expects only integers. |
| Input n as a very large number as string | Convert string representation of n to numeric type using appropriate libraries. |
| Requesting a Fibonacci sequence starting from an arbitrary point (not 0) | Adjust the iterative process to begin calculating the Fibonacci sequence from the desired index, and ensure correct handling of indices. |