L2 · Working engineerLists & arrays~5 min · 5 tests#15

Flatten a Nested List (One Level)

Flatten a list of lists into one flat Python list with a comprehension or itertools.chain. Understand why sum(lists, []) is secretly O(n²).

The problem

nums is a list of lists. Return a single flat list with all the inner values, in order.

(Only one level of nesting — the deep version is a recursion question later.)

Examples

  1. Example 1

    Input

    flatten([[1, 2], [3, 4], [5, 6]])

    Expected output

    [1, 2, 3, 4, 5, 6]
  2. Example 2

    Input

    flatten([[1], [], [2, 3]])

    Expected output

    [1, 2, 3]

+ 3 hidden tests on Submit.

Edge cases to ask about

  • Empty outer list
  • Empty inner lists

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · flatten
    ⌘/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
    sum(nums, [])O(n·k)O(n)Builds a brand-new list on every addition — quadratic in the number of inner lists.
    bestNested comprehension / itertools.chainO(n)O(n)Each value is copied exactly once.
    Walkthrough of the optimal approach (try it yourself first)

    [x for inner in nums for x in inner] — the for-clauses are in the same order as the nested loops you would write.

    sum(nums, []) looks clever but creates a new list for every +, so with many inner lists it is O(n²). Run both in the Complexity Lab with many short inner lists to see the gap.

    Complexity: O(n) time, O(n) space. n is the total number of inner values; each is copied once into the output.

    Reveal the reference solution
    def flatten(nums):
        return [x for inner in nums for x in inner]

    The brute force, for comparison

    def flatten(nums):
        return sum(nums, [])

    Follow-ups interviewers ask

    • Arbitrarily deep nesting (recursion).
    • Flatten lazily with a generator.

    Frequently asked interview questions

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

    What is the time complexity of Flatten a Nested List (One Level) in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. n is the total number of inner values; each is copied once into the output.

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

    sum(nums, []): O(n·k) time, O(n) space. Builds a brand-new list on every addition — quadratic in the number of inner lists. Nested comprehension / itertools.chain: O(n) time, O(n) space. Each value is copied exactly once.

    What follow-up questions do interviewers ask about Flatten a Nested List (One Level)?

    Arbitrarily deep nesting (recursion). Flatten lazily with a generator.