NumPy dtypes: why arrays are homogeneous and what that means for performance and memory

Quick Overview

Asks what a dtype is in NumPy, why every element of an array shares one type, and how that choice affects speed and memory use. It tests understanding of memory layout, strides, vectorized operations, overflow and precision limits, and type promotion.

NumPy dtypes: why arrays are homogeneous and what that means for performance and memory

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the concept of data types in NumPy arrays. Why are NumPy arrays homogeneous, and what are the implications for performance and memory? ```hint Finding element i Ask how NumPy computes where element i lives without looking at any other element, and what that requires of every element in the array. ``` ```hint Count the bytes Compare the memory for a million integers in an `int64` array and in a Python list, counting what each list slot actually points to. ``` ### What a Strong Answer Covers - What a dtype describes, and how it fixes the size and meaning of every element - How homogeneity enables address arithmetic, strides and views - The memory cost compared with a Python list, with numbers - Why typed, contiguous data makes vectorized operations fast - The costs of homogeneity: overflow, precision loss, type promotion, and object or string arrays ### Follow-up Questions - What does `np.array([1, "a"])` produce, and why? - When would you store a large feature matrix as `float32` instead of `float64`, and what can go wrong? - How can a structured dtype hold mixed fields while the array is still homogeneous? - Why does slicing a NumPy array return a view while fancy indexing returns a copy?

Overview: Asks what a dtype is in NumPy, why every element of an array shares one type, and how that choice affects speed and memory use. It tests understanding of memory layout, strides, vectorized operations, overflow and precision limits, and type promotion.

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

Explain the concept of data types in NumPy arrays. Why are NumPy arrays homogeneous, and what are the implications for performance and memory?

What a Strong Answer Covers Guidance

  • What a dtype describes, and how it fixes the size and meaning of every element
  • How homogeneity enables address arithmetic, strides and views
  • The memory cost compared with a Python list, with numbers
  • Why typed, contiguous data makes vectorized operations fast
  • The costs of homogeneity: overflow, precision loss, type promotion, and object or string arrays

Follow-up Questions Guidance

  • What does np.array([1, "a"]) produce, and why?
  • When would you store a large feature matrix as float32 instead of float64 , and what can go wrong?
  • How can a structured dtype hold mixed fields while the array is still homogeneous?
  • Why does slicing a NumPy array return a view while fancy indexing returns a copy?