Build a shape-preserving boolean membership mask for a NumPy array with np.isin

Quick Overview

Return a boolean mask shaped like a NumPy array of any dimension that marks which elements appear in a 1-D allowed list. It tests np.isin argument order, shape preservation, inverting the mask, and the sort and lookup-table strategies behind membership tests.

Build a shape-preserving boolean membership mask for a NumPy array with np.isin

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given an array `values` of arbitrary shape and a 1-D array `allowed`, return a boolean array shaped like `values` that is `True` exactly where the element appears in `allowed`. Requirements: - The output shape matches `values` (not flattened). - The result dtype is `bool`. - Use `np.isin`, no Python loops. For example: ```text Input: values = np.array([[1, 2, 3], [4, 5, 6]]) allowed = np.array([2, 4, 6]) Output: array([[False, True, False], [ True, False, True]]) ``` 2, 4 and 6 appear in `allowed`, so their positions are `True`; 1, 3 and 5 do not. The output keeps the `(2, 3)` shape of `values`. ```hint Argument order and shape Check which of the two arguments `np.isin` uses to decide the output shape, and pass the arrays in that order. ``` ### Constraints & Assumptions - `values` can have any number of dimensions, including a 0-d scalar array. - `allowed` is a 1-D array of the same kind of values (numbers or strings) and may contain duplicates. ### What a Strong Answer Covers - A single `np.isin` call with the arguments in the right order, returning a `bool` array shaped like `values`. - How the result keeps the shape of `values`, and how this differs from the older 1-D-only routine. - How to get the complementary mask. - The time and memory cost of the strategies NumPy can use, and how to choose one. - Inputs that give surprising results, such as NaN values. ### Follow-up Questions - How does `np.isin` differ from `np.in1d`? - How would you invert the mask to find disallowed elements? - What is the time complexity, and when does `np.isin` switch strategies?

Overview: Return a boolean mask shaped like a NumPy array of any dimension that marks which elements appear in a 1-D allowed list. It tests np.isin argument order, shape preservation, inverting the mask, and the sort and lookup-table strategies behind membership tests.

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

Given an array values of arbitrary shape and a 1-D array allowed, return a boolean array shaped like values that is True exactly where the element appears in allowed.

Requirements:

  • The output shape matches values (not flattened).
  • The result dtype is bool .
  • Use np.isin , no Python loops.

For example:

Input:  values  = np.array([[1, 2, 3], [4, 5, 6]])
        allowed = np.array([2, 4, 6])
Output: array([[False,  True, False],
               [ True, False,  True]])

2, 4 and 6 appear in allowed, so their positions are True; 1, 3 and 5 do not. The output keeps the (2, 3) shape of values.

Constraints & Assumptions

  • values can have any number of dimensions, including a 0-d scalar array.
  • allowed is a 1-D array of the same kind of values (numbers or strings) and may contain duplicates.

What a Strong Answer Covers Guidance

  • A single np.isin call with the arguments in the right order, returning a bool array shaped like values .
  • How the result keeps the shape of values , and how this differs from the older 1-D-only routine.
  • How to get the complementary mask.
  • The time and memory cost of the strategies NumPy can use, and how to choose one.
  • Inputs that give surprising results, such as NaN values.

Follow-up Questions Guidance

  • How does np.isin differ from np.in1d ?
  • How would you invert the mask to find disallowed elements?
  • What is the time complexity, and when does np.isin switch strategies?