What are NumPy strides and how do they let reshaping and transposing work without copying data?

Quick Overview

Asks what NumPy strides are and how they let reshape, transpose and slicing work without copying an array's data. Tests knowledge of ndarray memory layout, views versus copies, and the cases where NumPy has to copy.

What are NumPy strides and how do they let reshaping and transposing work without copying data?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What are NumPy strides, and how do they enable efficient array manipulations like reshaping and transposing without copying data? A NumPy array keeps its elements in one flat memory buffer, yet you can index it as a 2-D or N-D array and change its shape or axis order in constant time. ```hint Address arithmetic Work out the byte offset of element `a[i, j]` in a C-order `(3, 4)` int64 array. Which two numbers multiply `i` and `j`? ``` ```hint Look for the exception Flatten a transposed array with `reshape` and check `np.shares_memory` against the original. Ask why the result differs from reshaping the original array. ``` ### What a Strong Answer Covers - A definition of strides as byte steps per axis, with the formula that maps an index to a position in the flat buffer - A concrete stride tuple for a small array and how it changes under transpose and slicing - When reshape can return a view and when NumPy has to copy - Other features built on strides, such as broadcasting and sliding windows - The costs of working with views: shared writes, slower non-contiguous access, and libraries that need contiguous input ### Follow-up Questions - How do you check whether an operation returned a view or a copy, and why does it matter when you write to the result? - What strides does a reversed slice such as `a[::-1]` have, and what happens when you pass it to code that expects contiguous memory? - Why is `np.lib.stride_tricks.as_strided` risky, and what does `sliding_window_view` do to make the same idea safer? - When is it worth paying for `np.ascontiguousarray` on a transposed view?

Overview: Asks what NumPy strides are and how they let reshape, transpose and slicing work without copying an array's data. Tests knowledge of ndarray memory layout, views versus copies, and the cases where NumPy has to copy.

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

What are NumPy strides, and how do they enable efficient array manipulations like reshaping and transposing without copying data?

A NumPy array keeps its elements in one flat memory buffer, yet you can index it as a 2-D or N-D array and change its shape or axis order in constant time.

What a Strong Answer Covers Guidance

  • A definition of strides as byte steps per axis, with the formula that maps an index to a position in the flat buffer
  • A concrete stride tuple for a small array and how it changes under transpose and slicing
  • When reshape can return a view and when NumPy has to copy
  • Other features built on strides, such as broadcasting and sliding windows
  • The costs of working with views: shared writes, slower non-contiguous access, and libraries that need contiguous input

Follow-up Questions Guidance

  • How do you check whether an operation returned a view or a copy, and why does it matter when you write to the result?
  • What strides does a reversed slice such as a[::-1] have, and what happens when you pass it to code that expects contiguous memory?
  • Why is np.lib.stride_tricks.as_strided risky, and what does sliding_window_view do to make the same idea safer?
  • When is it worth paying for np.ascontiguousarray on a transposed view?