What is broadcasting in NumPy, what are its rules, and how does it avoid copying data?

Quick Overview

Asks what NumPy broadcasting is, which rules decide whether two array shapes are compatible, and how a stretched array is represented without copying data. Tests shape reasoning, stride-based views, and the silent bugs that come from broadcasting along the wrong axis.

What is broadcasting in NumPy, what are its rules, and how does it avoid copying data?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What is broadcasting in NumPy? Explain the broadcasting rules and how broadcasting allows operations between arrays of different shapes without copying data. ```hint Compare the shapes Write the two shapes one above the other and ask which pairs of sizes have to agree, and what happens when one array has fewer dimensions. ``` ```hint Memory layout Think about how an array's strides tell NumPy where the next element along each axis sits in memory. ``` ### What a Strong Answer Covers - A definition of broadcasting as element-wise operations between arrays whose shapes differ. - The shape compatibility rules, including how arrays with different numbers of dimensions are handled and when NumPy raises an error. - How the stretched array is represented without a copy, and what memory does get allocated. - At least one worked shape example, including a pair of shapes that fails. - Pitfalls such as 1-D versus column vectors and very large intermediate results. ### Follow-up Questions - Given `X` of shape (n, d) and `Y` of shape (m, d), how would you compute all pairwise squared Euclidean distances with broadcasting, and what does it cost in memory? - Why does `X - X.mean(axis=1)` fail on a (100, 4) matrix, and how do you fix it? - What does `np.broadcast_to` return, and why is the result read-only? - When would a different formulation beat a direct broadcast expression?

Overview: Asks what NumPy broadcasting is, which rules decide whether two array shapes are compatible, and how a stretched array is represented without copying data. Tests shape reasoning, stride-based views, and the silent bugs that come from broadcasting along the wrong axis.

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

What is broadcasting in NumPy? Explain the broadcasting rules and how broadcasting allows operations between arrays of different shapes without copying data.

What a Strong Answer Covers Guidance

  • A definition of broadcasting as element-wise operations between arrays whose shapes differ.
  • The shape compatibility rules, including how arrays with different numbers of dimensions are handled and when NumPy raises an error.
  • How the stretched array is represented without a copy, and what memory does get allocated.
  • At least one worked shape example, including a pair of shapes that fails.
  • Pitfalls such as 1-D versus column vectors and very large intermediate results.

Follow-up Questions Guidance

  • Given X of shape (n, d) and Y of shape (m, d), how would you compute all pairwise squared Euclidean distances with broadcasting, and what does it cost in memory?
  • Why does X - X.mean(axis=1) fail on a (100, 4) matrix, and how do you fix it?
  • What does np.broadcast_to return, and why is the result read-only?
  • When would a different formulation beat a direct broadcast expression?