Generate Fibonacci Sequence - Problem
Generate Fibonacci Sequence using JavaScript Generators

Create a generator function that yields the famous Fibonacci sequence infinitely. The Fibonacci sequence is one of the most beautiful patterns in mathematics, where each number is the sum of the two preceding numbers.

The sequence follows the rule: F(n) = F(n-1) + F(n-2)

Starting with 0, 1, the sequence produces: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...

Goal: Implement a generator function that can be called to create an iterator object. Each call to next() should return the next Fibonacci number in the sequence.

Key Requirements:
• Use JavaScript's function* syntax (generator function)
• Use the yield keyword to produce values
• Generate values on-demand (lazy evaluation)
• Handle infinite sequence generation efficiently

Input & Output

example_1.js — Basic Usage
$ Input: const gen = fibonacciGenerator(); for (let i = 0; i < 8; i++) { console.log(gen.next().value); }
Output: 0 1 1 2 3 5 8 13
💡 Note: The generator starts with 0 and 1, then each subsequent number is the sum of the previous two numbers. This demonstrates the basic usage of the Fibonacci generator.
example_2.js — Array Collection
$ Input: const gen = fibonacciGenerator(); const first10 = []; for (let i = 0; i < 10; i++) { first10.push(gen.next().value); } console.log(first10);
Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
💡 Note: This example shows how to collect multiple Fibonacci numbers into an array. The generator maintains its state between calls, producing the sequence correctly.
example_3.js — Large Numbers
$ Input: const gen = fibonacciGenerator(); // Skip to 15th number for (let i = 0; i < 15; i++) gen.next(); // Get next 3 numbers console.log(gen.next().value); console.log(gen.next().value); console.log(gen.next().value);
Output: 610 987 1597
💡 Note: Demonstrates that the generator can handle larger Fibonacci numbers and maintains state correctly even when we skip ahead in the sequence.

Constraints

  • The generator should use function* syntax
  • Must use the yield keyword to produce values
  • Should generate numbers on-demand (lazy evaluation)
  • Numbers may exceed JavaScript's safe integer range for very large sequences
  • Memory usage should be O(1) regardless of sequence length

Visualization

Tap to expand
Fibonacci Generator - Memory Efficient ApproachGenerator Memorycurr: 0next: 10yieldAfter Updatecurr: 1next: 11yieldAfter Updatecurr: 1next: 21yieldComplete Sequence:0112358...✓ Only 2 variables needed✓ O(1) space complexity✓ Infinite sequence support✓ Lazy evaluationPattern: current, next = next, current + next
Understanding the Visualization
1
Initialization
Start with current=0 and next=1, representing the first two Fibonacci numbers
2
Yield Current
Return the current value to the caller
3
Update State
Slide the window: current becomes next, next becomes current + next
4
Repeat
Continue the pattern infinitely, generating numbers on-demand
Key Takeaway
🎯 Key Insight: Generators with two-variable sliding window provide infinite Fibonacci sequences with optimal O(1) space and time complexity per number.
Asked in
Google 25 Amazon 18 Meta 22 Microsoft 15
23.4K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen