Min-max normalize a 1-D NumPy array to the range [0, 1]

Quick Overview

Write a NumPy function that rescales a 1-D array to the range 0 to 1 with min-max normalization. It tests vectorized arithmetic and broadcasting, handling of constant arrays and integer dtypes, and how to extend the scaling to each column of a 2-D array.

Min-max normalize a 1-D NumPy array to the range [0, 1]

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `minmax(a: np.ndarray) -> np.ndarray` that rescales a 1-D array to `[0, 1]` using `(a - a.min()) / (a.max() - a.min())`. The smallest value maps to `0` and the largest to `1`. ```python a = np.array([10., 20., 30., 50.]) minmax(a) # array([0. , 0.25, 0.5 , 1. ]) ``` ```hint Check the denominator Before you divide, work out what the formula gives for an input like `np.array([3., 3., 3.])`. ``` ### Constraints & Assumptions - `a` is a non-empty 1-D numeric NumPy array. - For the base task you may assume `a.max() > a.min()`. The follow-ups cover the constant case. - The result is a float array with the same shape as `a`. ### What a Strong Answer Covers - A vectorized implementation that computes the minimum and maximum once and returns a float array. - What the formula does when every value is equal, and a defined output for that case. - How the input dtype affects the result, including the range computed on integer inputs. - How to extend the scaling to each column of a 2-D array with the right axis and broadcasting. ### Follow-up Questions - How would you extend this to normalize each column of a 2-D array independently? - How would you guard against the constant-array case where `a.max() == a.min()`? - How would you handle `NaN` values in the input? - In a model pipeline, where should the minimum and maximum come from, and what happens to test values outside that range?

Overview: Write a NumPy function that rescales a 1-D array to the range 0 to 1 with min-max normalization. It tests vectorized arithmetic and broadcasting, handling of constant arrays and integer dtypes, and how to extend the scaling to each column of a 2-D array.

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

Write minmax(a: np.ndarray) -> np.ndarray that rescales a 1-D array to [0, 1] using (a - a.min()) / (a.max() - a.min()). The smallest value maps to 0 and the largest to 1.

a = np.array([10., 20., 30., 50.])
minmax(a)
# array([0.  , 0.25, 0.5 , 1.  ])

Constraints & Assumptions

  • a is a non-empty 1-D numeric NumPy array.
  • For the base task you may assume a.max() > a.min() . The follow-ups cover the constant case.
  • The result is a float array with the same shape as a .

What a Strong Answer Covers Guidance

  • A vectorized implementation that computes the minimum and maximum once and returns a float array.
  • What the formula does when every value is equal, and a defined output for that case.
  • How the input dtype affects the result, including the range computed on integer inputs.
  • How to extend the scaling to each column of a 2-D array with the right axis and broadcasting.

Follow-up Questions Guidance

  • How would you extend this to normalize each column of a 2-D array independently?
  • How would you guard against the constant-array case where a.max() == a.min() ?
  • How would you handle NaN values in the input?
  • In a model pipeline, where should the minimum and maximum come from, and what happens to test values outside that range?