Skip to content

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)?

  1. NumPy lazily creates a new buffer on first access.
  2. Transpose returns a view that shares the buffer; only shape and strides are swapped. The catch: result is no longer C-contiguous.
  3. NumPy uses copy-on-write like Linux processes.
  4. 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)

  1. Shapes align from the trailing axis.
  2. Two dimensions are compatible if equal or if one is 1.
  3. A length-1 axis is virtually replicated.
  4. (3,) broadcasts against (4, 3)(4, 3).
  5. (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

  1. Any is assignable to and from everything; once a value is Any, mypy can't reason about operations on it. Practical implication: numpy 2.x's better stubs let you use NDArray[float32], but legacy Any returns lose type info at the boundary.
  2. Any is a synonym for None.
  3. mypy --strict ignores Any.
  4. numpy is written in C and has no types.
Answer **Choice 1.** `Any` is the universal escape hatch — every operation on an `Any` is also `Any`. Annotate explicitly and narrow at library boundaries.