Threshold Coordinates With np.argwhere

Quick Overview

Return the row and column coordinates of every element in a 2-D NumPy array that is strictly greater than a threshold, using np.argwhere. The question tests boolean masks, the order and shape of the output, the empty case, and how np.argwhere compares with np.where.

Threshold Coordinates With np.argwhere

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a 2-D array `arr` and a scalar `threshold`, return the (row, column) coordinates of every element strictly greater than `threshold`, using `np.argwhere`. Requirements: - Return an `(N, 2)` integer array for elements `> threshold` (strict, not `>=`). - Order the coordinates row-major (top to bottom, left to right). - If no element qualifies, return an array of shape `(0, 2)`. For example: ```python arr = np.array([[1, 5, 2], [7, 0, 9]]) threshold = 4 coords_above(arr, threshold) # array([[0, 1], # [1, 0], # [1, 2]]) ``` ```hint Order and the empty case Work out the order in which `np.argwhere` reports matches and the shape it returns when nothing matches, then compare both with the requirements. ``` ### Constraints & Assumptions - `arr` is a 2-D numeric NumPy array (or a nested list that `np.asarray` can convert). - `threshold` is a scalar, and the comparison is strict. ### What a Strong Answer Covers - Building a boolean mask with a vectorized comparison and passing it to `np.argwhere`. - Why the output is in row-major order with shape `(N, 2)`, including the `(0, 2)` empty case. - The strict `>` comparison and how NaN entries behave. - Time and memory cost, including the temporary mask. ### Follow-up Questions - How does `np.argwhere` differ from `np.where`? - How would you also return the matching values alongside the coordinates? - What changes if `arr` has three or more dimensions?

Overview: Return the row and column coordinates of every element in a 2-D NumPy array that is strictly greater than a threshold, using np.argwhere. The question tests boolean masks, the order and shape of the output, the empty case, and how np.argwhere compares with np.where.

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

Given a 2-D array arr and a scalar threshold, return the (row, column) coordinates of every element strictly greater than threshold, using np.argwhere.

Requirements:

  • Return an (N, 2) integer array for elements > threshold (strict, not >= ).
  • Order the coordinates row-major (top to bottom, left to right).
  • If no element qualifies, return an array of shape (0, 2) .

For example:

arr = np.array([[1, 5, 2],
                [7, 0, 9]])
threshold = 4

coords_above(arr, threshold)
# array([[0, 1],
#        [1, 0],
#        [1, 2]])

Constraints & Assumptions

  • arr is a 2-D numeric NumPy array (or a nested list that np.asarray can convert).
  • threshold is a scalar, and the comparison is strict.

What a Strong Answer Covers Guidance

  • Building a boolean mask with a vectorized comparison and passing it to np.argwhere .
  • Why the output is in row-major order with shape (N, 2) , including the (0, 2) empty case.
  • The strict > comparison and how NaN entries behave.
  • Time and memory cost, including the temporary mask.

Follow-up Questions Guidance

  • How does np.argwhere differ from np.where ?
  • How would you also return the matching values alongside the coordinates?
  • What changes if arr has three or more dimensions?