Boolean Indexing and Filtering

~8 mincode completion

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 numbers

  • threshold: Scalar cutoff value

  • Return 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 min

8 employers weight this skill

3 frontier labs, 2 quant funds, 2 big tech firms, 1 autonomy company. Top match scores 68.

Python
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
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.