How do you reshape a NumPy array, and what constraints apply, such as the total number of elements?

Quick Overview

Asks how to reshape a NumPy array, what happens to the data and its metadata during a reshape, and which constraints the new shape must meet. Tests understanding of element counts, inferring a dimension with -1, views versus copies, and row-major versus column-major order.

How do you reshape a NumPy array, and what constraints apply, such as the total number of elements?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How can you reshape a NumPy array? Explain the conceptual process behind a reshape and the constraints the new shape has to satisfy, such as the total number of elements. ```hint Data versus description An ndarray is a block of memory plus a description of how to walk through it. Ask which of the two a reshape has to change, and when the existing memory layout makes that impossible without moving data. ``` ### What a Strong Answer Covers - The element-count rule and what happens when a requested shape breaks it - How `-1` lets NumPy infer one dimension, and the limits on using it - When `reshape` returns a view and when it falls back to a copy, and why that matters when you write to the result - How `order='C'` and `order='F'` change which element lands in which position ### Follow-up Questions - How would you check whether the result of a reshape shares memory with the original array? - What is the difference between `ravel()` and `flatten()`, and when would you pick each? - Why does reshaping a transposed array usually produce a copy? - How does `reshape` differ from `np.resize` when the requested size does not match the array?

Overview: Asks how to reshape a NumPy array, what happens to the data and its metadata during a reshape, and which constraints the new shape must meet. Tests understanding of element counts, inferring a dimension with -1, views versus copies, and row-major versus column-major order.

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

How can you reshape a NumPy array? Explain the conceptual process behind a reshape and the constraints the new shape has to satisfy, such as the total number of elements.

What a Strong Answer Covers Guidance

  • The element-count rule and what happens when a requested shape breaks it
  • How -1 lets NumPy infer one dimension, and the limits on using it
  • When reshape returns a view and when it falls back to a copy, and why that matters when you write to the result
  • How order='C' and order='F' change which element lands in which position

Follow-up Questions Guidance

  • How would you check whether the result of a reshape shares memory with the original array?
  • What is the difference between ravel() and flatten() , and when would you pick each?
  • Why does reshaping a transposed array usually produce a copy?
  • How does reshape differ from np.resize when the requested size does not match the array?