Problem · Design
Fibonacci Iterator
Learn this problemProblem statement
Implement an iterable class named FibonacciIterator that:
- Accepts an integer parameter
nin its constructor, representing the number of Fibonacci numbers to generate. - When iterated, yields the first
nFibonacci numbers in sequence.
The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding numbers.
Function
fibonacciIterator(n: int) → int[]Complete the class FibonacciIterator in the editor with the following parameter:
int n: the number of Fibonacci numbers to generate
Returns
iterable: an iterable with the firstnFibonacci numbers
FastPrep runner note (June 30, 2026): The original prompt asks for an iterable class named FibonacciIterator. To make this practice runnable here, return the values that iteration would yield as an integer array. 🐣
Examples
Example 1
n = 4return = [0, 1, 1, 2]For n = 4, the source example iterates through the object as follows:
fibonacci = FibonacciIterator(4)
for num in fibonacci:
print(num, end=' ')The above code should output 0 1 1 2.
In the FastPrep runner, return the same values as [0, 1, 1, 2].