New: Python & JavaScript Support

Programming is logic, not copying
Learn to think like an expert

Every solution includes a detailed analysis: what, how, and most importantly – why.

No credit card required

See the difference

Not just an answer — understand the thought process behind every solution.

Problem

Fibonacci sequence iterator

Write an iterator class that generates Fibonacci numbers up to a specified maximum value.

Example:

Input: fib = FibonacciIterator(20)
Output: 0, 1, 1, 2, 3, 5, 8, 13
Solution + Detailed explanation

Iterator implementation

The iterator uses class state to store the current values of the sequence. We initialize current = 0 and next_value = 1. In the __next__() method, we check if current > max_value. If so, we raise StopIteration. Otherwise, we return current and update values: current = next_value, next_value = current + next_value. Time complexity is O(n)O(n), where nn is the number of generated values.

Choose language

Get precise explanations tailored to the syntax and style of your programming language.

More languages coming soon: Go, Rust, TypeScript, and Ruby.

Ask Lucid