L2 · Working engineerLists & arrays~6 min · 5 tests#14

Common Elements in Three Lists

Find the values present in all three Python lists, in the first list's order, using set intersection in linear time. Run the tests in your browser.

The problem

Return the values that appear in all three lists, in the order they appear in a, each once.

Examples

  1. Example 1

    Input

    common_three([1, 2, 3, 4, 5], [2, 3, 5, 7], [3, 5, 8, 9])

    Expected output

    [3, 5]
  2. Example 2

    Input

    common_three([1, 2], [2, 1], [1])

    Expected output

    [1]

+ 3 hidden tests on Submit.

Edge cases to ask about

  • Any list empty
  • Duplicates
  • No overlap

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · common_three
    ⌘/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
    List lookupsO(n·(m+p))O(1)Each in scans a list.
    Sets for b and cO(n + m + p)O(m + p)Two sets, one scan of a.
    bestThree pointers (sorted inputs)O(n + m + p)O(1)Advance whichever pointer holds the smallest value.
    Walkthrough of the optimal approach (try it yourself first)

    Turn b and c into sets, then walk a keeping values found in both (and not already output). Linear time overall.

    If the interviewer says the lists are sorted, switch to three pointers: when all three match, record it; otherwise advance the pointer with the smallest value.

    Complexity: O(n + m + p) time, O(m + p) space. Building two sets and scanning the first list are each linear; the sets are the extra memory.

    Reveal the reference solution
    def common_three(a, b, c):
        in_b, in_c = set(b), set(c)
        seen = set()
        out = []
        for x in a:
            if x in in_b and x in in_c and x not in seen:
                seen.add(x)
                out.append(x)
        return out

    The brute force, for comparison

    def common_three(a, b, c):
        out = []
        for x in a:
            if x in b and x in c and x not in out:
                out.append(x)
        return out

    Follow-ups interviewers ask

    • The lists are sorted — use O(1) extra space.
    • Generalise to k lists.

    Frequently asked interview questions

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

    What is the time complexity of Common Elements in Three Lists in Python?

    The optimal solution runs in O(n + m + p) time and O(m + p) auxiliary space. Building two sets and scanning the first list are each linear; the sets are the extra memory.

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

    List lookups: O(n·(m+p)) time, O(1) space. Each `in` scans a list. Sets for b and c: O(n + m + p) time, O(m + p) space. Two sets, one scan of a. Three pointers (sorted inputs): O(n + m + p) time, O(1) space. Advance whichever pointer holds the smallest value.

    What follow-up questions do interviewers ask about Common Elements in Three Lists?

    The lists are sorted — use O(1) extra space. Generalise to k lists.