When can you add two NumPy arrays together, and what conditions enable broadcasting?

Quick Overview

Asks when two NumPy arrays can be added element-wise and which shape rules allow broadcasting. Tests comparing shapes from the right, the role of size-1 and missing dimensions, and spotting shape combinations that fail or quietly produce a result of the wrong shape.

When can you add two NumPy arrays together, and what conditions enable broadcasting?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

When can you add two NumPy arrays together, and what conditions enable broadcasting? ```hint Line up the shapes Write the two shapes one above the other, aligned on the right, and compare them one column at a time. ``` ### What a Strong Answer Covers - The rule for comparing two shapes and how the result shape is formed - How a missing leading dimension and a dimension of size 1 are treated - Examples that broadcast and examples that raise an error - Shape combinations that broadcast without error but give an unintended result ### Follow-up Questions - How would you subtract each row's mean from a matrix, and what goes wrong on a square matrix if you leave out `keepdims=True`? - Why does `y_true - y_pred` return an (n, n) array when one has shape (n,) and the other (n, 1)? - Does broadcasting copy the smaller array in memory, and what changes with an in-place `+=`?

Overview: Asks when two NumPy arrays can be added element-wise and which shape rules allow broadcasting. Tests comparing shapes from the right, the role of size-1 and missing dimensions, and spotting shape combinations that fail or quietly produce a result of the wrong shape.

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

When can you add two NumPy arrays together, and what conditions enable broadcasting?

What a Strong Answer Covers Guidance

  • The rule for comparing two shapes and how the result shape is formed
  • How a missing leading dimension and a dimension of size 1 are treated
  • Examples that broadcast and examples that raise an error
  • Shape combinations that broadcast without error but give an unintended result

Follow-up Questions Guidance

  • How would you subtract each row's mean from a matrix, and what goes wrong on a square matrix if you leave out keepdims=True ?
  • Why does y_true - y_pred return an (n, n) array when one has shape (n,) and the other (n, 1)?
  • Does broadcasting copy the smaller array in memory, and what changes with an in-place += ?