What is a NumPy ndarray and what makes it efficient?

Quick Overview

Explain what a NumPy ndarray is and why operations on it run much faster than on Python lists. It tests your understanding of dtypes, memory layout, strides and views, vectorized compiled loops, and the cases where those advantages disappear.

What is a NumPy ndarray and what makes it efficient?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What is a NumPy `ndarray` and what makes it efficient? ```hint Compare with a Python list Think about what a Python list of a million integers actually stores in memory, and what a Python loop over it has to do for every element. ``` ### What a Strong Answer Covers - The structure of an ndarray: a typed data buffer plus metadata such as shape, dtype and strides - Why homogeneous, compactly stored values are smaller and faster than a Python list of objects - How strides turn slices, transposes and many reshapes into views without copying - Vectorized operations running as compiled loops, and broadcasting - Cases where the efficiency disappears, such as object dtype, Python-level loops or repeated appends ### Follow-up Questions - Why is calling `np.append` inside a loop slow, and what would you do instead? - When does `reshape` return a copy instead of a view, and how can you check which one you got? - What happens to speed and memory when an array has `dtype=object`? - How does broadcasting add a row vector to every row of a matrix without building the expanded array?

Overview: Explain what a NumPy ndarray is and why operations on it run much faster than on Python lists. It tests your understanding of dtypes, memory layout, strides and views, vectorized compiled loops, and the cases where those advantages disappear.

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

What is a NumPy ndarray and what makes it efficient?

What a Strong Answer Covers Guidance

  • The structure of an ndarray: a typed data buffer plus metadata such as shape, dtype and strides
  • Why homogeneous, compactly stored values are smaller and faster than a Python list of objects
  • How strides turn slices, transposes and many reshapes into views without copying
  • Vectorized operations running as compiled loops, and broadcasting
  • Cases where the efficiency disappears, such as object dtype, Python-level loops or repeated appends

Follow-up Questions Guidance

  • Why is calling np.append inside a loop slow, and what would you do instead?
  • When does reshape return a copy instead of a view, and how can you check which one you got?
  • What happens to speed and memory when an array has dtype=object ?
  • How does broadcasting add a row vector to every row of a matrix without building the expanded array?