Min-Max Scale a Pandas Series to [0, 1]

Quick Overview

Implement a pandas function that min-max scales a numeric Series to the range 0 to 1 using an exact formula, without mutating the input. It tests vectorized Series arithmetic, keeping the index, name and float64 dtype, and reasoning about edge cases such as a zero range or missing values.

Min-Max Scale a Pandas Series to [0, 1]

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `minmax_scale(s: pd.Series) -> pd.Series`. Given a numeric Series, return a new Series rescaled to `[0, 1]` using exactly `(s - s.min()) / (s.max() - s.min())`. The function must meet these requirements: - Do not mutate `s`. - Preserve the original index and name. - The result dtype is `float64`, and the values must match the formula under `np.allclose`. For example: ```python s = pd.Series([10, 20, 30], name="x") minmax_scale(s) # 0 0.0 # 1 0.5 # 2 1.0 # Name: x, dtype: float64 ``` ```hint Use vectorized arithmetic Arithmetic between a Series and a scalar already returns a new Series on the same index. Check what that gives you for free, and which dtype comes out for integer and `float32` input. ``` ### Constraints & Assumptions - `s` has at least two distinct values, so the denominator is never zero. - NaN values and single-value inputs are out of scope. ### What a Strong Answer Covers - A vectorized implementation of the exact formula that returns a new object and leaves the input untouched. - How the index, name and `float64` dtype are kept for integer and float inputs. - The time and space cost of the approach. - What happens on inputs outside the stated scope, such as a zero range or missing values. ### Follow-up Questions - How would you handle a constant Series where `s.max() == s.min()`? - How would you generalize this to scale to an arbitrary `[a, b]` range? - If the scaled column feeds a model, which data should the minimum and maximum come from at training time and at prediction time? - How should NaN values be treated when computing the minimum and maximum?

Overview: Implement a pandas function that min-max scales a numeric Series to the range 0 to 1 using an exact formula, without mutating the input. It tests vectorized Series arithmetic, keeping the index, name and float64 dtype, and reasoning about edge cases such as a zero range or missing values.

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

Implement minmax_scale(s: pd.Series) -> pd.Series. Given a numeric Series, return a new Series rescaled to [0, 1] using exactly (s - s.min()) / (s.max() - s.min()).

The function must meet these requirements:

  • Do not mutate s .
  • Preserve the original index and name.
  • The result dtype is float64 , and the values must match the formula under np.allclose .

For example:

s = pd.Series([10, 20, 30], name="x")
minmax_scale(s)
# 0    0.0
# 1    0.5
# 2    1.0
# Name: x, dtype: float64

Constraints & Assumptions

  • s has at least two distinct values, so the denominator is never zero.
  • NaN values and single-value inputs are out of scope.

What a Strong Answer Covers Guidance

  • A vectorized implementation of the exact formula that returns a new object and leaves the input untouched.
  • How the index, name and float64 dtype are kept for integer and float inputs.
  • The time and space cost of the approach.
  • What happens on inputs outside the stated scope, such as a zero range or missing values.

Follow-up Questions Guidance

  • How would you handle a constant Series where s.max() == s.min() ?
  • How would you generalize this to scale to an arbitrary [a, b] range?
  • If the scaled column feeds a model, which data should the minimum and maximum come from at training time and at prediction time?
  • How should NaN values be treated when computing the minimum and maximum?