Skip to content
Tech Interview Prep
Technical interview guide

Core Data Structures

Lists, tuples, dicts, and sets: their underlying implementations and when each is the right choice.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v3
Editorial status
Reviewed

Scope: Python 3.14; notes distinguish language guarantees from CPython implementation behavior.

Overview

Curated: · Written: · Reviewed:

Core Data Structures

In one line

Lists, tuples, dicts and sets are containers of references. Choose each by its mutability, ordering, uniqueness and lookup cost: a list for ordered mutable sequences, a dict for keyed lookup, a set or frozenset for membership. Language guarantees are stated as such below; CPython details are marked as implementation behaviour. Current to Python 3.14.

Key definitions

  • Aliasing: two names bind one object. Assignment copies no data.
  • Shallow copy: a new outer container holding the same nested references (a.copy(), a[:]).
  • Hashable: has a hash that stays fixed for its lifetime and an equality consistent with that hash.
  • Equality class: the values a set treats as one element.
  • Dict view: a live window onto keys(), values() or items(), not a snapshot.
  • Sentinel: a private object that tells absent apart from a stored None.

Formulas and complexity

x∈set≈O(1) average,x∈list=O(n)x \in \text{set} \approx O(1)\text{ average}, \quad x \in \text{list} = O(n) ∣A∪B∣ costs O(∣A∣+∣B∣),A∩B costs O(min⁡(∣A∣,∣B∣)),A∖B costs O(∣A∣)|A \cup B| \text{ costs } O(|A|+|B|), \quad A \cap B \text{ costs } O(\min(|A|,|B|)), \quad A \setminus B \text{ costs } O(|A|)
Operationlistdequedict / set
x in cO(n)O(n)O(1) average
append / pop at rightO(1) amortizedO(1)n/a
insert / pop at leftO(n)O(1)n/a
c[i]O(1)O(n) near middlen/a

How it works

  • A list or tuple stores references. A tuple's slots are fixed, but a list inside it can still change.
  • Lookup in a dict or set: the hash picks a starting slot, probing walks candidates, and __eq__ confirms the match. Collisions are resolved by equality, never by the hash alone.
  • A deleted slot keeps a dummy marker so later probe chains pass through it. Dummies count toward the fill that triggers a resize, so a delete-heavy table gets rebuilt on a later insert (CPython behaviour).
  • Dicts keep insertion order (language guarantee since 3.7). Sets make no order promise.
yes no yes no key hash key index into slots slot empty? miss stored hash and key equal? hit probe next slot

When to use and when not to

NeedChooseNot when
Ordered, mutable, indexed sequencelistyou need a fixed record
Fixed record usable as a dict keytuple of hashable membersany member is a list or dict
Keyed lookup that keeps insertion orderdictyou need no values
Repeated membership testsset or frozenseta list scanned in a loop
Ordered uniquenessdict.fromkeys(...)a set, which promises no order
FIFO or both-end operationsdequeyou index or slice the middle
Stable value key for a permission setfrozensetmembers are mutable

Traps interviewers probe

  1. b = a is not a copy. Both names change together. Fix: a.copy() or a comment saying the sharing is intended.
  2. [[0] * w] * h shares one row. Fix: [[0] * w for _ in range(h)].
  3. def f(items=[]) keeps state across calls. The default is built once at definition time. Fix: default to None, then allocate inside the function.
  4. if not value treats None, [] and 0 alike. Fix: test is None, or use a private sentinel with get(name, sentinel).
  5. A tuple holding a list is treated as hashable. The tuple itself fails hash(). Fix: check every nested member, or normalize to a frozenset or tuple of tuples.

Interview tips

Open by naming the contract the data needs (ordering, uniqueness, mutability, dominant operation), then the container that meets it.

Follow-ups to expect: shallow versus copy.deepcopy; why in on a list is linear; how equality resolves hash collisions; why frozenset suits a cache key; why Counter keeps zero and negative counts and needs explicit deletion.

30-second outline: "The workload needs X and Y, so I use Z. Z gives O(1) average lookup because the hash locates a slot and equality confirms it. Its invariant is that the key's hash stays fixed. The trap I would check is aliasing or nested mutable state. I would profile before changing anything else."

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.