Per-Row Argmax Indices in NumPy

Quick Overview

Return the column index of the maximum value in each row of a 2D NumPy array, with ties going to the lowest index. It tests how the axis argument of a reduction works, the shape and dtype of the result, and how argmax behaves on ties and missing values.

Per-Row Argmax Indices in NumPy

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `argmax_per_row(arr)`. Given a 2-D NumPy array `arr` of shape `(m, n)`, return a 1-D array of length `m` where element `i` is the column index of the maximum in row `i`. Requirements: - Return a 1-D `ndarray` of integer indices. - Use `np.argmax` with `axis=1`. - Ties resolve to the first (lowest) column index. For example: ```python arr = np.array([[1, 5, 3], [9, 2, 2], [4, 4, 7]]) argmax_per_row(arr) # array([1, 0, 2]) ``` ```hint Output shape The result has one entry per row. Work out which of the two dimensions of `(m, n)` the reduction has to remove to leave that shape. ``` ### Constraints & Assumptions - Input is 2-D with at least one column. - Values are numeric (int or float). ### What a Strong Answer Covers - Which axis gets collapsed and why that yields one index per row - The shape and dtype of the returned array - How ties at the maximum are resolved - Behavior on awkward inputs such as `nan` values or an array with zero rows ### Follow-up Questions - How would you get the max value per row, not just the index? - What changes for `argmin` or a different axis? - How would you return the indices of the top k values in each row, largest first?

Overview: Return the column index of the maximum value in each row of a 2D NumPy array, with ties going to the lowest index. It tests how the axis argument of a reduction works, the shape and dtype of the result, and how argmax behaves on ties and missing values.

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

Implement argmax_per_row(arr). Given a 2-D NumPy array arr of shape (m, n), return a 1-D array of length m where element i is the column index of the maximum in row i.

Requirements:

  • Return a 1-D ndarray of integer indices.
  • Use np.argmax with axis=1 .
  • Ties resolve to the first (lowest) column index.

For example:

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

Constraints & Assumptions

  • Input is 2-D with at least one column.
  • Values are numeric (int or float).

What a Strong Answer Covers Guidance

  • Which axis gets collapsed and why that yields one index per row
  • The shape and dtype of the returned array
  • How ties at the maximum are resolved
  • Behavior on awkward inputs such as nan values or an array with zero rows

Follow-up Questions Guidance

  • How would you get the max value per row, not just the index?
  • What changes for argmin or a different axis?
  • How would you return the indices of the top k values in each row, largest first?