In-Place Masked Negation of a NumPy Array

Quick Overview

Write a NumPy function that flips the sign of every element strictly between 3 and 8 in place and returns the same array object. It tests boolean masks built from compound conditions, operator precedence with the & operator, and how an indexed in-place update differs from rebinding a name.

In-Place Masked Negation of a NumPy Array

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `negate_middle(arr)` that flips the sign of every element strictly between 3 and 8, in place, and returns the same array object (not a copy). ```python arr = np.array([2, 4, 8, 5, 9, 3]) negate_middle(arr) # arr -> [ 2, -4, 8, -5, 9, 3] ``` ```hint What the update expands to Work out what `arr[mask] *= -1` becomes in terms of `__getitem__` and `__setitem__`, and which of those calls touches the caller's buffer. ``` ### Constraints & Assumptions - Select with `(arr > 3) & (arr < 8)`, so the boundaries 3 and 8 stay unchanged. - Negate with in-place multiplication, for example `arr[mask] *= -1`. - Do not reassign `arr`: mutate the passed-in object and return it. - `arr` is a numeric NumPy array. An integer dtype is fine. ### What a Strong Answer Covers - A mask built from strict comparisons that leaves 3 and 8 untouched - Elementwise `&` with parentheses, and why `and` or a chained comparison fails on arrays - Why the indexed in-place update reaches the caller's array when rebinding the name would not - Dtype edge cases such as unsigned integers and NaN values ### Follow-up Questions - Why does `arr[mask] *= -1` mutate in place while `arr = arr * mask_factor` may not? - How would you extend this to negate values outside the range instead? - Does `arr[mask] *= -1` allocate temporary arrays, and how could you reduce them?

Overview: Write a NumPy function that flips the sign of every element strictly between 3 and 8 in place and returns the same array object. It tests boolean masks built from compound conditions, operator precedence with the & operator, and how an indexed in-place update differs from rebinding a name.

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

Write negate_middle(arr) that flips the sign of every element strictly between 3 and 8, in place, and returns the same array object (not a copy).

arr = np.array([2, 4, 8, 5, 9, 3])
negate_middle(arr)
# arr -> [ 2, -4,  8, -5,  9,  3]

Constraints & Assumptions

  • Select with (arr > 3) & (arr < 8) , so the boundaries 3 and 8 stay unchanged.
  • Negate with in-place multiplication, for example arr[mask] *= -1 .
  • Do not reassign arr : mutate the passed-in object and return it.
  • arr is a numeric NumPy array. An integer dtype is fine.

What a Strong Answer Covers Guidance

  • A mask built from strict comparisons that leaves 3 and 8 untouched
  • Elementwise & with parentheses, and why and or a chained comparison fails on arrays
  • Why the indexed in-place update reaches the caller's array when rebinding the name would not
  • Dtype edge cases such as unsigned integers and NaN values

Follow-up Questions Guidance

  • Why does arr[mask] *= -1 mutate in place while arr = arr * mask_factor may not?
  • How would you extend this to negate values outside the range instead?
  • Does arr[mask] *= -1 allocate temporary arrays, and how could you reduce them?