Filter a NumPy array to values above a threshold with a boolean mask

Quick Overview

Write a NumPy function that returns the elements of a 1-D array strictly greater than a threshold, in their original order, without a Python loop. It tests boolean masking, strict comparison, copy semantics and how empty results and list inputs are handled.

Filter a NumPy array to values above a threshold with a boolean mask

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a 1-D NumPy array `arr` and a scalar `threshold`, return a new 1-D array of the elements strictly greater than `threshold`, in original order. Requirements: - Index with the boolean mask `arr > threshold`: no explicit Python loop or comprehension. - Values equal to `threshold` are excluded. - Return a filtered copy: the input need not be modified in place. - An empty result returns an empty array, not an error. Example: ```python arr = np.array([1, 5, 3, 8, 2]) threshold = 3 # Output: array([5, 8]) ``` ```hint Check the mask Print `arr > threshold` before you index with it and look at its dtype and shape. Then ask what the same expression does when `arr` arrives as a plain Python list. ``` ### What a Strong Answer Covers - Builds the mask with one vectorized comparison and indexes the array with it, with no Python loop. - Uses a strict comparison so values equal to the threshold are dropped. - Explains that boolean indexing keeps the original order and returns a new array. - Handles an empty result and array-like input such as a Python list. - States the time and extra space as linear in the array length. ### Follow-up Questions - How would you keep only the elements greater than `low` and at most `high`? - How would you return the positions of the matching elements instead of their values? - What happens to NaN values in the input, and how would you keep them if the caller needs them? - How would you set the elements at or below the threshold to 0 while keeping the array's length?

Overview: Write a NumPy function that returns the elements of a 1-D array strictly greater than a threshold, in their original order, without a Python loop. It tests boolean masking, strict comparison, copy semantics and how empty results and list inputs are handled.

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

Given a 1-D NumPy array arr and a scalar threshold, return a new 1-D array of the elements strictly greater than threshold, in original order.

Requirements:

  • Index with the boolean mask arr > threshold : no explicit Python loop or comprehension.
  • Values equal to threshold are excluded.
  • Return a filtered copy: the input need not be modified in place.
  • An empty result returns an empty array, not an error.

Example:

arr = np.array([1, 5, 3, 8, 2])
threshold = 3
# Output: array([5, 8])

What a Strong Answer Covers Guidance

  • Builds the mask with one vectorized comparison and indexes the array with it, with no Python loop.
  • Uses a strict comparison so values equal to the threshold are dropped.
  • Explains that boolean indexing keeps the original order and returns a new array.
  • Handles an empty result and array-like input such as a Python list.
  • States the time and extra space as linear in the array length.

Follow-up Questions Guidance

  • How would you keep only the elements greater than low and at most high ?
  • How would you return the positions of the matching elements instead of their values?
  • What happens to NaN values in the input, and how would you keep them if the caller needs them?
  • How would you set the elements at or below the threshold to 0 while keeping the array's length?