L2 · Working engineerPython internals~4 min · 3 tests#65

Mutable vs Immutable: List Aliasing

Understand why y = x shares one Python list, then write a function that adds an item without mutating its input. Mutable vs immutable, tested live.

The problem

After x = [1, 2, 3]; y = x; y.append(4), both x and y are [1, 2, 3, 4] — they name the same list.

Write append_safely(items, item) that returns a new list with item added at the end and leaves items unchanged.

Examples

  1. Example 1

    Input

    probe(append_safely)

    Expected output

    ([1, 2, 3], [1, 2, 3, 4], False)
  2. Example 2

    Input

    alias_demo()

    Expected output

    ([1, 2, 3, 4], [1, 2, 3, 4], True)

+ 1 hidden test on Submit.

Edge cases to ask about

  • Caller's list must not change
  • Identity vs equality
How the tests call your code

These helpers run before your code. The test inputs above call them.

def probe(append_safely):
    x = [1, 2, 3]
    y = append_safely(x, 4)
    return (x, y, x is y)

def alias_demo():
    x = [1, 2, 3]
    y = x
    y.append(4)
    return (x, y, x is y)

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · append_safely
    ⌘/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 new listO(n)O(n)items + [item] or [*items, item].
    Walkthrough of the optimal approach (try it yourself first)

    Python variables are names bound to objects. y = x binds a second name to the same list, so y.append(4) is visible through x. Lists, dicts and sets are mutable; ints, strings and tuples are immutable — "changing" one actually creates a new object.

    Returning items + [item] leaves the caller's list alone, which is what a function that "returns a new list" promises.

    Complexity: O(n) time, O(n) space. Creating the new list copies the n existing references.

    Reveal the reference solution
    def append_safely(items, item):
        return items + [item]

    Follow-ups interviewers ask

    • Why does x += [4] mutate but x = x + [4] does not?
    • What happens when you pass a list into a function?

    Frequently asked interview questions

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

    What is the time complexity of Mutable vs Immutable: List Aliasing in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. Creating the new list copies the n existing references.

    What follow-up questions do interviewers ask about Mutable vs Immutable: List Aliasing?

    Why does x += [4] mutate but x = x + [4] does not? What happens when you pass a list into a function?