Clip Negatives to Zero With np.where

Quick Overview

Replace every negative value in a NumPy array with 0 using the three-argument form of np.where, and leave the input array unchanged. It tests boolean masks, argument order in np.where, copying versus in-place updates, and how NumPy picks the result dtype.

Clip Negatives to Zero With np.where

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a NumPy array of numbers, return a new array of the same shape in which values below 0 become 0 and values greater than or equal to 0 are unchanged. Use the three-argument form `np.where(cond, a, b)`, and do not modify the input array. ```python arr = np.array([-3, 2, -1, 0, 5]) # expected output: array([0, 2, 0, 0, 5]) ``` ```hint Start from a mask Build a boolean array that marks the elements to replace, then decide which value `np.where` should pick on each side of that mask. ``` ### Constraints & Assumptions - The input can have any shape. The example is 1-D. - The values can be integers or floats. ### What a Strong Answer Covers - The condition and the order of the second and third arguments to `np.where`. - Why the result is a new array and the input stays unchanged. - The dtype of the result for integer and float inputs. - What happens to special values such as `NaN` and `-0.0`. - Time and space complexity. ### Follow-up Questions - How would you clip to an arbitrary lower bound `lo`? - How would you do the replacement in place when memory is tight, and what does that change for the caller? - What happens if the function receives a Python list, and how would you handle it?

Overview: Replace every negative value in a NumPy array with 0 using the three-argument form of np.where, and leave the input array unchanged. It tests boolean masks, argument order in np.where, copying versus in-place updates, and how NumPy picks the result dtype.

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

Given a NumPy array of numbers, return a new array of the same shape in which values below 0 become 0 and values greater than or equal to 0 are unchanged.

Use the three-argument form np.where(cond, a, b), and do not modify the input array.

arr = np.array([-3, 2, -1, 0, 5])
# expected output: array([0, 2, 0, 0, 5])

Constraints & Assumptions

  • The input can have any shape. The example is 1-D.
  • The values can be integers or floats.

What a Strong Answer Covers Guidance

  • The condition and the order of the second and third arguments to np.where .
  • Why the result is a new array and the input stays unchanged.
  • The dtype of the result for integer and float inputs.
  • What happens to special values such as NaN and -0.0 .
  • Time and space complexity.

Follow-up Questions Guidance

  • How would you clip to an arbitrary lower bound lo ?
  • How would you do the replacement in place when memory is tight, and what does that change for the caller?
  • What happens if the function receives a Python list, and how would you handle it?