How can you programmatically check if a NumPy array is a view or a copy, for example with .base or __array_interface__?

Quick Overview

Asks how to tell in code whether a NumPy array is a view or a copy, using attributes such as .base, ownership flags and the array interface data pointer. Tests whether you know what each check proves and which NumPy operations share memory with their input.

How can you programmatically check if a NumPy array is a view or a copy, for example with .base or __array_interface__?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How can you programmatically check if a NumPy array is a view or a copy, for example using `.base` or `__array_interface__`? A view shares its data buffer with another array, so a write through the view changes the original. A copy has its own buffer. ```hint Two different questions Separate "does this array own its data?" from "do these two arrays overlap in memory?", and ask whether two overlapping arrays must start at the same address. ``` ### What a Strong Answer Covers - What `.base` holds for an array that owns its data, for a view and for a copy, and what a non-None base does and does not prove - How to test whether two specific arrays overlap, and how the exact check differs from the fast one - What the data pointer in `__array_interface__` can and cannot tell you - Which common operations return views and which return copies, and why the check matters in practice ### Follow-up Questions - Some operations return a view only when they can. Which ones, and how would you detect which case you got? - Why can a small slice keep a very large array alive in memory, and how do you avoid it? - How does this relate to chained assignment and copy-on-write in pandas? - How would you write a function that is guaranteed never to modify its input array?

Overview: Asks how to tell in code whether a NumPy array is a view or a copy, using attributes such as .base, ownership flags and the array interface data pointer. Tests whether you know what each check proves and which NumPy operations share memory with their input.

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

How can you programmatically check if a NumPy array is a view or a copy, for example using .base or __array_interface__?

A view shares its data buffer with another array, so a write through the view changes the original. A copy has its own buffer.

What a Strong Answer Covers Guidance

  • What .base holds for an array that owns its data, for a view and for a copy, and what a non-None base does and does not prove
  • How to test whether two specific arrays overlap, and how the exact check differs from the fast one
  • What the data pointer in __array_interface__ can and cannot tell you
  • Which common operations return views and which return copies, and why the check matters in practice

Follow-up Questions Guidance

  • Some operations return a view only when they can. Which ones, and how would you detect which case you got?
  • Why can a small slice keep a very large array alive in memory, and how do you avoid it?
  • How does this relate to chained assignment and copy-on-write in pandas?
  • How would you write a function that is guaranteed never to modify its input array?