Compute the Peak-to-Peak Range of Each Column in a NumPy Array

Quick Overview

Return the max-minus-min range of each column of a 2-D NumPy array in a single call. It tests reductions along an axis, the shape of the result, and pitfalls such as small signed integer dtypes and NaN values.

Compute the Peak-to-Peak Range of Each Column in a NumPy Array

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a 2-D NumPy array `arr`, return a 1-D array with the peak-to-peak range (max minus min) of each column, computed in a single NumPy call. Use `np.ptp` with `axis=0`. The result length equals the number of columns. For example: ```python arr = [[1, 5, 9], [4, 2, 6], [7, 8, 3]] # Output: [6, 6, 6] per column: 7-1, 8-2, 9-3 ``` ```hint Watch the dtype Think about what dtype the result has when `arr` is `int8` and one column holds both -100 and 100. ``` ### What a Strong Answer Covers - The single call and the axis it reduces over. - Why the result has one entry per column, checked against the example. - The dtype of the result and what goes wrong with small signed integer types. - What happens with `NaN` values or an array with no rows. ### Follow-up Questions - How would you get the per-row range instead? - If a column contains `NaN`, what does `np.ptp` return, and how would you compute the range while ignoring it? - How would you use the column ranges to min-max scale every column to the interval from 0 to 1?

Overview: Return the max-minus-min range of each column of a 2-D NumPy array in a single call. It tests reductions along an axis, the shape of the result, and pitfalls such as small signed integer dtypes and NaN values.

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

Given a 2-D NumPy array arr, return a 1-D array with the peak-to-peak range (max minus min) of each column, computed in a single NumPy call. Use np.ptp with axis=0. The result length equals the number of columns.

For example:

arr = [[1, 5, 9],
       [4, 2, 6],
       [7, 8, 3]]
# Output: [6, 6, 6]   per column: 7-1, 8-2, 9-3

What a Strong Answer Covers Guidance

  • The single call and the axis it reduces over.
  • Why the result has one entry per column, checked against the example.
  • The dtype of the result and what goes wrong with small signed integer types.
  • What happens with NaN values or an array with no rows.

Follow-up Questions Guidance

  • How would you get the per-row range instead?
  • If a column contains NaN , what does np.ptp return, and how would you compute the range while ignoring it?
  • How would you use the column ranges to min-max scale every column to the interval from 0 to 1?