L2 · Working engineerPython internals~5 min · 5 tests#59

Fibonacci Generator With yield

Write a Python generator that yields the first n Fibonacci numbers lazily with yield, using O(1) memory. Learn how generators differ from lists.

The problem

Write a generator function fib_gen(n) that yields the first n Fibonacci numbers: 0, 1, 1, 2, 3, 5, ….

It must be a generator (use yield), not a function that returns a list.

Examples

  1. Example 1

    Input

    list(fib_gen(7))

    Expected output

    [0, 1, 1, 2, 3, 5, 8]
  2. Example 2

    Input

    type(fib_gen(3)).__name__

    Expected output

    'generator'

+ 3 hidden tests on Submit — lazy: values on demand.

Edge cases to ask about

  • n = 0
  • n = 1
  • Partial consumption with next()

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · fib_gen
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Build a listO(n)O(n)Holds every value at once.
    bestGenerator with yieldO(n)O(1)Produces one value at a time, on demand.
    Walkthrough of the optimal approach (try it yourself first)

    A function with yield returns a generator: calling it runs nothing; each next() resumes the function until the next yield. Only a and b live in memory, so fib_gen(10**9) costs nothing until you iterate.

    Generators can be consumed once — a second list(g) is empty.

    Complexity: O(n) time, O(1) space. Each value costs O(1) to produce and only two numbers are kept in memory, however large n is.

    Reveal the reference solution
    def fib_gen(n):
        a, b = 0, 1
        for _ in range(n):
            yield a
            a, b = b, a + b

    Follow-ups interviewers ask

    • An infinite generator plus itertools.islice.
    • What does yield from do?

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Fibonacci Generator With yield in Python?

    The optimal solution runs in O(n) time and O(1) auxiliary space. Each value costs O(1) to produce and only two numbers are kept in memory, however large n is.

    What is the brute-force approach, and how do you optimise it?

    Build a list: O(n) time, O(n) space. Holds every value at once. Generator with yield: O(n) time, O(1) space. Produces one value at a time, on demand.

    What follow-up questions do interviewers ask about Fibonacci Generator With yield?

    An infinite generator plus itertools.islice. What does yield from do?