L2 · Working engineerSets~3 min · 4 tests#37

Common Elements Using Sets

Use Python set intersection (&) to find common elements of two lists in O(n+m), returning them sorted. Learn set operators with live test cases.

The problem

Return the values found in both a and b, as a sorted list without duplicates. Use set operations.

Examples

  1. Example 1

    Input

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

    Expected output

    [3, 4]
  2. Example 2

    Input

    common_sorted([9, 1, 9], [1, 9])

    Expected output

    [1, 9]

+ 2 hidden tests on Submit.

Edge cases to ask about

  • No overlap
  • Duplicates
  • Empty list

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · common_sorted
    ⌘/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
    Nested scansO(n × m)O(1)
    bestSet intersectionO(n + m + k log k)O(n + m)& is O(min(n, m)); sorting the k results adds k log k.
    Walkthrough of the optimal approach (try it yourself first)

    set(a) & set(b) (or set(a).intersection(b)) gives the common values; sorted() makes the order deterministic. Know the four operators: | union, & intersection, - difference, ^ symmetric difference.

    Complexity: O(n + m) time, O(n + m) space. Building both sets is linear and & is O(min(n, m)); sorting only the k common values adds k log k.

    Reveal the reference solution
    def common_sorted(a, b):
        return sorted(set(a) & set(b))

    Follow-ups interviewers ask

    • Keep the order of a instead of sorting (question 8).

    Frequently asked interview questions

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

    What is the time complexity of Common Elements Using Sets in Python?

    The optimal solution runs in O(n + m) time and O(n + m) auxiliary space. Building both sets is linear and & is O(min(n, m)); sorting only the k common values adds k log k.

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

    Nested scans: O(n × m) time, O(1) space. Set intersection: O(n + m + k log k) time, O(n + m) space. & is O(min(n, m)); sorting the k results adds k log k.

    What follow-up questions do interviewers ask about Common Elements Using Sets?

    Keep the order of a instead of sorting (question 8).