Explain NumPy broadcasting rules and when they cause unexpected results or shape mismatch errors

Quick Overview

Asks how NumPy decides whether two array shapes can be broadcast together and where the rules cause errors or silent mistakes. Tests shape alignment, stride-0 views, keepdims and the habits that catch wrong-shape bugs in numerical Python code.

Explain NumPy broadcasting rules and when they cause unexpected results or shape mismatch errors

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the internal rules of broadcasting in NumPy and when it might lead to unexpected results or shape mismatch errors. ```hint Line the shapes up Write the two shapes one above the other, right-aligned, and compare them column by column. ``` ```hint Look for the missing error The worst broadcasting bugs never raise. Think of two shapes that are compatible under the rules but not what you meant, such as a column of labels and a flat vector of predictions. ``` ### What a Strong Answer Covers - The rule itself: right alignment, padding the shorter shape with ones on the left, each pair equal or one of them 1, and the size of each result axis - How broadcasting avoids copying data (stride-0 views) - When it raises an error and how to read the message - Silent cases that produce a wrong shape or wrong values, with a concrete example - Habits and checks that catch these bugs early ### Follow-up Questions - Why does `a += b` fail in some cases where `a + b` works? - How would you compute all pairwise distances between two sets of points with broadcasting, and what limits the input size? - How would you write a function that rejects ambiguous inputs, such as an `(n, 1)` array where it expects `(n,)`? - When would you reach for `np.einsum` instead of chained broadcasting?

Overview: Asks how NumPy decides whether two array shapes can be broadcast together and where the rules cause errors or silent mistakes. Tests shape alignment, stride-0 views, keepdims and the habits that catch wrong-shape bugs in numerical Python code.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the internal rules of broadcasting in NumPy and when it might lead to unexpected results or shape mismatch errors.

What a Strong Answer Covers Guidance

  • The rule itself: right alignment, padding the shorter shape with ones on the left, each pair equal or one of them 1, and the size of each result axis
  • How broadcasting avoids copying data (stride-0 views)
  • When it raises an error and how to read the message
  • Silent cases that produce a wrong shape or wrong values, with a concrete example
  • Habits and checks that catch these bugs early

Follow-up Questions Guidance

  • Why does a += b fail in some cases where a + b works?
  • How would you compute all pairwise distances between two sets of points with broadcasting, and what limits the input size?
  • How would you write a function that rejects ambiguous inputs, such as an (n, 1) array where it expects (n,) ?
  • When would you reach for np.einsum instead of chained broadcasting?