Problem · Design

Fibonacci Iterator

Learn this problem
EasyNvidiaOA

Problem statement

Implement an iterable class named FibonacciIterator that:

  • Accepts an integer parameter n in its constructor, representing the number of Fibonacci numbers to generate.
  • When iterated, yields the first n Fibonacci 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 first n Fibonacci 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].

More Nvidia problems

drafts saved locally
public int[] fibonacciIterator(int n) {
  // FastPrep runner: return the values yielded by FibonacciIterator.
  // write your code here
}
n4
expected[0, 1, 1, 2]
checking account