Neural Mastery
← Back to Practice

Batch Dot Product

Difficulty: Medium · Concept: Linear Algebra — Vectors

A harder variant of Dot Product From Scratch: instead of one pair of vectors, you're given two equal-length batches of vectors, and need the dot product of each corresponding pair — exactly the shape of computation that shows up whenever a model scores many examples at once (a batch of query vectors against a batch of key vectors, a batch of embeddings against a batch of candidates) instead of one at a time.

The added difficulty isn't the math — it's validation with two different failure modes to catch: the two batches might contain a different number of vectors, or an individual pair within the batch might have mismatched dimensions (a batch that's supposed to be uniform but isn't).

Your task: implement batch_dot_product(batch_a, batch_b). Both are lists of vectors (lists of numbers). Raise ValueError if the batches have different lengths, or if any corresponding pair of vectors has different lengths from each other.

Implement it yourself
assert batch_dot_product([[1, 2, 3], [4, 5, 6]], [[1, 0, 0], [0, 1, 0]]) == [1, 5] assert batch_dot_product([[1, 1]], [[2, 3]]) == [5] assert batch_dot_product([], []) == [] assert batch_dot_product([[1, 2], [3, 4], [5, 6]], [[2, 2], [1, 1], [0, 0]]) == [6, 7, 0] assert batch_dot_product([[-1, 1]], [[1, -1]]) == [-2]

Next: Matrix Multiplication From Scratch

Last updated Sep 5, 2026Edit this pageReport an issue