L2 · Working engineerPython internals~3 min · 5 tests#60

List vs Tuple vs Set vs Dict

Pick the right Python data structure — list, tuple, set or dict — from its properties: ordered, mutable, unique, key-value. Concept check with tests.

The problem

Write pick_structure(ordered, mutable, unique, key_value) that returns the name of the built-in that fits the requirements: "list", "tuple", "set" or "dict".

need answer
key → value lookups "dict"
only unique values (order not needed) "set"
ordered and immutable "tuple"
ordered and mutable "list"

Check them in that priority order.

Examples

  1. Example 1

    Input

    pick_structure(ordered=True, mutable=True, unique=False, key_value=False)

    Expected output

    'list'
  2. Example 2

    Input

    pick_structure(ordered=True, mutable=False, unique=False, key_value=False)

    Expected output

    'tuple'
  3. Example 3

    Input

    pick_structure(ordered=False, mutable=True, unique=True, key_value=False)

    Expected output

    'set'

+ 2 hidden tests on Submit.

Edge cases to ask about

  • Hashability: tuple keys vs list keys

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · pick_structure
    ⌘/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
    Decision rulesO(1)O(1)
    Walkthrough of the optimal approach (try it yourself first)
    ordered mutable unique lookup by x in
    list yes yes no index O(n)
    tuple yes no no index O(n)
    set no yes yes — O(1)
    dict insertion order yes unique keys key O(1)

    Tuples are hashable (if their contents are), so they can be dict keys and set members; lists cannot. Since Python 3.7 dicts preserve insertion order.

    Complexity: O(1) time, O(1) space. A fixed number of checks.

    Reveal the reference solution
    def pick_structure(ordered, mutable, unique, key_value):
        if key_value:
            return "dict"
        if unique:
            return "set"
        if ordered and not mutable:
            return "tuple"
        return "list"

    Follow-ups interviewers ask

    • Why can't a list be a dict key?
    • What is a frozenset for?

    Frequently asked interview questions

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

    What is the time complexity of List vs Tuple vs Set vs Dict in Python?

    The optimal solution runs in O(1) time and O(1) auxiliary space. A fixed number of checks.

    What follow-up questions do interviewers ask about List vs Tuple vs Set vs Dict?

    Why can't a list be a dict key? What is a frozenset for?