Min-Max Scale a 1D Feature to [0, 1]

Basics
BasicsFeature ScalingNumPyGoogleMetaAmazon

Problem

Implement minmax_scale_1d(x) to linearly rescale a 1D numeric array into [0, 1]. If all values are identical (max == min), return an array of zeros of the same length.

Why It Matters: Min-Max scaling bounds features to [0, 1] so distance-based models (KNN, K-Means, SVMs) and neural nets aren't dominated by large-magnitude columns.

Sample Inputs & Expected Outputs

Example 1 — Standard range

>>> minmax_scale_1d([10, 20, 30])
array([0.0, 0.5, 1.0])

min=10, max=30 -> (20 - 10) / 20 = 0.5.

Example 2 — Negative to positive

>>> minmax_scale_1d([-10, 0, 10])
array([0.0, 0.5, 1.0])

Works cleanly across negative numbers.

Example 3 — Constant array edge case

>>> minmax_scale_1d([5, 5, 5])
array([0.0, 0.0, 0.0])

When max == min, returns all zeros instead of dividing by zero.

Constraints & Edge Cases

  • Return a 1D float NumPy array.

Constraints & Evaluation Criteria

  • Return a 1D float NumPy array.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[10, 20, 30]
Expected Output / Criteria
[0.0, 0.5, 1.0]