Return the row and column of the largest element in a 2-D NumPy array

Quick Overview

Given a 2-D NumPy array, return the row and column of its largest element as a tuple of built-in Python integers, without loops. It tests how a flat index maps back to a multi-dimensional position, how NumPy breaks ties, and care with NumPy scalar types.

Return the row and column of the largest element in a 2-D NumPy array

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a 2-D NumPy array, return the position of its largest element as a tuple `(row, col)` of two Python `int`s. Requirements: - Use `np.argmax` on the flattened array, then `np.unravel_index` (no nested loops). - Convert the NumPy scalar coordinates to built-in `int` before returning. For example: ```python a = np.array([[3, 7, 2], [9, 4, 5]]) argmax_2d_coords(a) # (1, 0), since a[1, 0] == 9 is the largest ``` ```hint What the flat index means Without an `axis` argument, `np.argmax` counts positions as if the array were one long row. Work out which shape `np.unravel_index` needs to undo that count. ``` ### What a Strong Answer Covers - What `np.argmax` returns for a 2-D array when no axis is given, and why that value is not yet a coordinate. - How the flat index is turned back into a row and a column, and why the two steps must agree on element order. - Converting the NumPy integer results to built-in `int`, and what can go wrong if that step is skipped. - Behavior on ties, NaN values and an empty array. - Time and extra space cost. ### Follow-up Questions - How does `argmax` break ties when the maximum appears more than once? - How would you extend this to return the argmax along a single axis? - How would you return the coordinates of every occurrence of the maximum? - What changes if the array can contain NaN values that should be ignored?

Overview: Given a 2-D NumPy array, return the row and column of its largest element as a tuple of built-in Python integers, without loops. It tests how a flat index maps back to a multi-dimensional position, how NumPy breaks ties, and care with NumPy scalar types.

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

Given a 2-D NumPy array, return the position of its largest element as a tuple (row, col) of two Python ints.

Requirements:

  • Use np.argmax on the flattened array, then np.unravel_index (no nested loops).
  • Convert the NumPy scalar coordinates to built-in int before returning.

For example:

a = np.array([[3, 7, 2],
              [9, 4, 5]])
argmax_2d_coords(a)  # (1, 0), since a[1, 0] == 9 is the largest

What a Strong Answer Covers Guidance

  • What np.argmax returns for a 2-D array when no axis is given, and why that value is not yet a coordinate.
  • How the flat index is turned back into a row and a column, and why the two steps must agree on element order.
  • Converting the NumPy integer results to built-in int , and what can go wrong if that step is skipped.
  • Behavior on ties, NaN values and an empty array.
  • Time and extra space cost.

Follow-up Questions Guidance

  • How does argmax break ties when the maximum appears more than once?
  • How would you extend this to return the argmax along a single axis?
  • How would you return the coordinates of every occurrence of the maximum?
  • What changes if the array can contain NaN values that should be ignored?