TRT
Sign in
mediumFoundationsconcept

How are `list` and `dict` implemented inside CPython, why is `dict` ordered since Python 3.7, and how do Key-Sharing Dicts (PEP 412) and `__slots__` save.

Walk through `PyListObject`'s `ob_item` pointer array, `PyDictObject`'s split `indices` + `entries` tables, and instance `__dict__` vs `__slots__`.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read

A strong answer

A strong candidate frames How are list and dict implemented inside CPython, why is dict ordered since Python 3.7, and how do Key-Sharing Dicts (PEP 412) and __slots__ save. around the exact memory layout, compiler semantics, and hardware execution model covered in [CPython List and Dict Internals: Over-Allocation and Compact Hash Tables]. Walk through PyListObject's ob_item pointer array, PyDictObject's split indices + entries tables, and instance __dict__ vs __slots__.

You store 1 million user records as standard Python dict objects and notice memory usage dropped by 35% after upgrading from an legacy sparse hash table, while iteration suddenly runs in insertion order and 2x faster. Meanwhile, inserting elements at the front of a list with lst.insert(0, x) in a loop of 100,000 items freezes your worker for 4 seconds!

When engineers encounter this bottleneck in production, a surface-level API description rarely explains what went wrong. In real workloads, every line of code executes against concrete hardware constraints: fixed-width memory cells, CPU cache lines, kernel descriptor tables, network socket buffers, or accelerator memory buses. Without a clear mental model of CPython PyListObject growth and compact PyDictObject layout, debugging degrades into guesswork—tuning timeouts, adding retries, or scaling instances blindly while the underlying architectural flaw remains untouched.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Trace PyListObject geometric over-allocation (~1.125x) and explain why pop(0) costs O(N).
  • Mastery of core invariant: Explain how CPython's compact dict separates sparse indices from dense insertion-ordered entries.
  • Mastery of core invariant: Reduce memory overhead on millions of class instances using @dataclass(slots=True) and __slots__.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How much RAM does adding `__slots__ = ('x', 'y')` save when you instantiate 5 million objects of a class?
  2. 02Why does mutating a `dict` (adding or deleting keys) while iterating over it raise `RuntimeError: dictionary changed size during iteration`?

Not started

Sign in to save your learning progress.

Sign in to save