The Generate Fibonacci Sequence interview question asks you to implement a function (often a generator in JavaScript or Python) that yields the Fibonacci sequence. The sequence starts with 0 and 1, and each subsequent number is the sum of the previous two (). You are typically asked to generate the sequence indefinitely or up to a certain .
Google and Bloomberg use this "Easy" problem to test your understanding of Generators and basic iterative logic. It evaluation whether you know how to maintain state across function calls without using global variables. While simple, it often serves as a warm-up before moving on to more complex questions involving memoization or dynamic programming optimizations.
This problem uses a simple Iterative pattern or a Generator pattern.
a = 0 and b = 1.a.a becomes the old b, and the new b becomes the sum of the old a and b.
This approach uses space and time.a = 0, b = 1. Yield 0.a = 1, b = 0 + 1 = 1. Yield 1.a = 1, b = 1 + 1 = 2. Yield 1.a = 2, b = 1 + 2 = 3. Yield 2.a = 3, b = 2 + 3 = 5. Yield 3.f(n) = f(n-1) + f(n-2) without memoization, which leads to complexity.Understand the difference between a "function" and a "generator." Generators are memory-efficient because they produce values on the fly (lazy evaluation) rather than computing everything upfront.