L1 · FoundationsNumbers & loops~3 min · 4 tests

FizzBuzz in Python

Solve FizzBuzz in Python: return 1..n with multiples of 3 as Fizz, 5 as Buzz and both as FizzBuzz. The classic first interview question, run live.

The problem

Return a list of strings for the numbers 1..n:

  • multiples of both 3 and 5 → "FizzBuzz"
  • multiples of 3 → "Fizz"
  • multiples of 5 → "Buzz"
  • anything else → the number as a string

Examples

  1. Example 1

    Input

    fizzbuzz(5)

    Expected output

    ['1', '2', 'Fizz', '4', 'Buzz']
  2. Example 2

    Input

    fizzbuzz(15)

    Expected output

    ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz']

+ 2 hidden tests on Submit.

Edge cases to ask about

  • n = 0
  • n = 1

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · fizzbuzz
    ⌘/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 measure your code against the optimal one at growing input sizes.

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

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

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

    ApproachTimeSpaceIdea
    Check 15 firstO(n)O(n)The most specific condition must come first.
    Walkthrough of the optimal approach (try it yourself first)

    Loop from 1 to n and test the most specific condition first: divisible by 15 (both 3 and 5), then 3, then 5. Checking 3 first would print "Fizz" for 15.

    It's an easy question, but interviewers are watching for clean conditions and an off-by-one-free range.

    Complexity: O(n) time, O(n) space. One loop iteration per number, and the output list holds n strings.

    Reveal the reference solution
    def fizzbuzz(n):
        out = []
        for i in range(1, n + 1):
            if i % 15 == 0:
                out.append("FizzBuzz")
            elif i % 3 == 0:
                out.append("Fizz")
            elif i % 5 == 0:
                out.append("Buzz")
            else:
                out.append(str(i))
        return out

    Follow-ups interviewers ask

    • Add 'Bazz' for multiples of 7 without a pile of elifs.

    Frequently asked interview questions

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

    What is the time complexity of FizzBuzz in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. One loop iteration per number, and the output list holds n strings.

    What follow-up questions do interviewers ask about FizzBuzz in Python?

    Add 'Bazz' for multiples of 7 without a pile of elifs.