Diagonal Stripe Gradient: Build an i + j Grid in NumPy Without Loops

Quick Overview

Build an m by n NumPy integer array whose entry at row i and column j equals i + j, with no Python loops. It tests broadcasting a column of row indices against a row of column indices, np.fromfunction and its default dtype, empty shapes, and overflow-safe dtype choices.

Diagonal Stripe Gradient: Build an i + j Grid in NumPy Without Loops

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `gradient(m, n)` returning an integer NumPy array of shape `(m, n)` where element `[i, j]` equals `i + j`. Every anti-diagonal of the result holds one constant value, which gives the array its striped look. Requirements: - Use vectorized construction (`np.fromfunction` or broadcasting of `np.arange`). No `for` or `while` loops and no comprehensions over cells. - The returned array must have an integer dtype. For example: ```python gradient(3, 4) # array([[0, 1, 2, 3], # [1, 2, 3, 4], # [2, 3, 4, 5]]) ``` ```hint Watch the dtype Check which dtype `np.fromfunction` uses for the index grids it builds when you do not pass one, and compare it with what `np.arange` returns for integer arguments. ``` ### Constraints & Assumptions - `m` and `n` are non-negative integers, so either dimension can be 0. - The result must be a full `(m, n)` array with an integer dtype. ### What a Strong Answer Covers - A loop-free construction, either `np.fromfunction` or a column of row indices broadcast against a row of column indices - An integer dtype on the result, including how the float default of `np.fromfunction` is handled - The output shape when `m` or `n` is 0 - The time and memory cost of the result ### Follow-up Questions - Why does the `np.fromfunction` callback receive whole arrays instead of scalars? - How would you control the output dtype and avoid overflow for large `m` and `n`? - How would you generalize this to an array of any number of dimensions, where each entry is the sum of its indices?

Overview: Build an m by n NumPy integer array whose entry at row i and column j equals i + j, with no Python loops. It tests broadcasting a column of row indices against a row of column indices, np.fromfunction and its default dtype, empty shapes, and overflow-safe dtype choices.

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

Write gradient(m, n) returning an integer NumPy array of shape (m, n) where element [i, j] equals i + j. Every anti-diagonal of the result holds one constant value, which gives the array its striped look.

Requirements:

  • Use vectorized construction ( np.fromfunction or broadcasting of np.arange ). No for or while loops and no comprehensions over cells.
  • The returned array must have an integer dtype.

For example:

gradient(3, 4)
# array([[0, 1, 2, 3],
#        [1, 2, 3, 4],
#        [2, 3, 4, 5]])

Constraints & Assumptions

  • m and n are non-negative integers, so either dimension can be 0.
  • The result must be a full (m, n) array with an integer dtype.

What a Strong Answer Covers Guidance

  • A loop-free construction, either np.fromfunction or a column of row indices broadcast against a row of column indices
  • An integer dtype on the result, including how the float default of np.fromfunction is handled
  • The output shape when m or n is 0
  • The time and memory cost of the result

Follow-up Questions Guidance

  • Why does the np.fromfunction callback receive whole arrays instead of scalars?
  • How would you control the output dtype and avoid overflow for large m and n ?
  • How would you generalize this to an array of any number of dimensions, where each entry is the sum of its indices?