Compute the outer product of two 1-D arrays using only broadcasting — no np.outer. Master this and you have the all-pairs primitive that powers attention scores, KNN distances, similarity matrices, and every config sweep in this track.
Implement: def outer(a, b) where a is shape (M,) and b is shape (N,). Return shape (M, N) where out[i, j] = a[i] * b[j].
Constraint: Do not use np.outer or np.einsum. The point is the indexing pattern.
The mechanic:
a[:, None] reshapes (M,) to (M, 1) — a column vector.b[None, :] reshapes (N,) to (1, N) — a row vector.(M, 1) * (1, N) broadcasts to (M, N) — the full outer grid.None (equivalently np.newaxis) inserts a new axis of size 1. After insertion, broadcasting fills it.
Why this matters: Every "compute X for all pairs of A and B" operation in numpy looks like A[..., None, :] OP B[..., :, None]. Attention scores: Q[..., :, None, :] * K[..., None, :, :] then sum. Pairwise distances: np.sum((X[:, None, :] - Y[None, :, :])**2, axis=-1). The "config grid" problems later in this track all use this same trick.
Math
Asked at
import numpy as np
def outer(...):
pass
Premium problem
Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.
Already premium?