English · Español
Phase 6 — Quizzes¶
🇪🇸 Espejo legible de
data/quizzes/phase-06-python-engineering.yaml. Las preguntas se centran en el modelo de memoria de NumPy (strides, broadcasting) y los matices de tipos.
Source of truth: data/quizzes/phase-06-python-engineering.yaml.
q-06-01 — Why is transpose O(1)?¶
- NumPy lazily creates a new buffer on first access.
- Transpose returns a view that shares the buffer; only shape and strides are swapped. The catch: result is no longer C-contiguous.
- NumPy uses copy-on-write like Linux processes.
- Transpose is in C and is always fast.
Answer
**Choice 2.** `(shape, strides)` swap from `((3,4),(16,4))` to `((4,3),(4,16))`. The `C_CONTIGUOUS=False` flag is the catch — code that assumes row-major silently degrades.q-06-02 — Broadcasting (multi-choice)¶
- Shapes align from the trailing axis.
- Two dimensions are compatible if equal or if one is 1.
- A length-1 axis is virtually replicated.
(3,)broadcasts against(4, 3)→(4, 3).(3,)broadcasts against(3, 4)→(3, 4).
Answer
**Choices 1, 2, 3, 4.** Choice 5 fails: `(3,)` aligns to the trailing axis (size 4), incompatible. Reshape to `(3, 1)` to broadcast against `(3, 4)`.q-06-03 — Byte offset calculation (free)¶
int32, shape (3, 4), strides (16, 4). Byte offset of element (2, 3)?
Answer
`offset = 2 · 16 + 3 · 4 = 32 + 12 = `**44** bytes.q-06-04 — GIL and CPU-bound work (free)¶
Answer
NumPy `matmul` **releases the GIL** during the BLAS call — the heavy work runs in C without the lock, threads parallelize. Pure-Python loops hold the GIL on every bytecode op, so threads serialize; for CPU-bound pure-Python work, use multiprocessing.q-06-05 — Any and mypy --strict¶
Anyis assignable to and from everything; once a value isAny, mypy can't reason about operations on it. Practical implication: numpy 2.x's better stubs let you useNDArray[float32], but legacyAnyreturns lose type info at the boundary.Anyis a synonym forNone.- mypy --strict ignores
Any. - numpy is written in C and has no types.