Boolean Indexing and Filtering
Implement filter_above(arr, threshold) that returns a new 1D array containing only elements strictly greater than threshold, in their original order.
Do not use a Python loop, use boolean indexing.
Examples
Filter values above 3
- Input
- filter_above([1, 5, 2, 8, 3], 3)
- Output
- [5, 8]
All elements pass
- Input
- filter_above([4, 7, 9], 0)
- Output
- [4, 7, 9]
No elements pass
- Input
- filter_above([1, 2, 3], 5)
- Output
- []
Hints
Hint 1
Index the array with a boolean mask to keep only the elements that match.
Hint 2
Reach for gt rather than gte.
Requirements
arr: 1D NumPy array of numbersthreshold: Scalar cutoff valueReturn 1D array of elements > threshold, preserving order.
Use a fully vectorised implementation without Python loops
Constraints
Vectorised implementation only, no Python loops
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
8 employers weight this skill
3 frontier labs, 2 quant funds, 2 big tech firms, 1 autonomy company. Top match scores 68.
import numpy as np
def filter_above(arr: np.ndarray, threshold: float) -> np.ndarray:
"""
Return elements of arr that are strictly greater than threshold.
Args:
arr: 1D NumPy array of numbers
threshold: Scalar cutoff value
Returns:
1D array of elements > threshold, preserving order.
"""
# YOUR CODE HERE
pass