Sum the 4x4 blocks of a 16x16 NumPy array with np.add.reduceat

Quick Overview

Sum the non-overlapping 4x4 blocks of a 16x16 NumPy array without Python loops by applying np.add.reduceat along both axes. It tests how reduceat splits an axis into segments, how two reductions compose, and when a reshape-based block sum is the better fit.

Sum the 4x4 blocks of a 16x16 NumPy array with np.add.reduceat

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a `16x16` NumPy array, return a `4x4` array where entry `[i, j]` is the sum of the block spanning rows `4i..4i+3` and columns `4j..4j+3`. Requirements: - Use `np.add.reduceat` with indices `[0, 4, 8, 12]` along `axis=0` and `axis=1`. - No explicit Python loops over blocks. For example, with `a = np.arange(256).reshape(16, 16)` the top-left block covers rows 0 to 3 and columns 0 to 3, and its values sum to 408. The expected output is: ```text [[ 408, 472, 536, 600], [1432, 1496, 1560, 1624], [2456, 2520, 2584, 2648], [3480, 3544, 3608, 3672]] ``` An all-ones `16x16` integer array gives a `4x4` array filled with 16, since every block holds 16 ones. ```hint Track the shapes Work out the shape of the result after one `reduceat` call along `axis=0`, then decide what a second call has to do to that result. ``` ### Constraints & Assumptions - The input is a numeric array of shape `(16, 16)`. - Blocks are `4x4`, do not overlap, and tile the array exactly. - The output has shape `(4, 4)`; the input does not need to be modified. ### What a Strong Answer Covers - A correct two-step use of `np.add.reduceat`, with the order of the calls and the shape of the intermediate result. - How `reduceat` turns a list of start indices into segments, including the segment that starts at the last index. - A check of the result against a known input. - The time and space cost of the vectorized approach. - When a reshape-based block sum is the better choice. ### Follow-up Questions - How does `reduceat` define each segment, and what happens at the last index? - How would you generalize to arbitrary block sizes or a reshape-based approach? - What does your function return for an `18x18` input, and is that what a caller would expect?

Overview: Sum the non-overlapping 4x4 blocks of a 16x16 NumPy array without Python loops by applying np.add.reduceat along both axes. It tests how reduceat splits an axis into segments, how two reductions compose, and when a reshape-based block sum is the better fit.

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

Given a 16x16 NumPy array, return a 4x4 array where entry [i, j] is the sum of the block spanning rows 4i..4i+3 and columns 4j..4j+3.

Requirements:

  • Use np.add.reduceat with indices [0, 4, 8, 12] along axis=0 and axis=1 .
  • No explicit Python loops over blocks.

For example, with a = np.arange(256).reshape(16, 16) the top-left block covers rows 0 to 3 and columns 0 to 3, and its values sum to 408. The expected output is:

[[ 408,  472,  536,  600],
 [1432, 1496, 1560, 1624],
 [2456, 2520, 2584, 2648],
 [3480, 3544, 3608, 3672]]

An all-ones 16x16 integer array gives a 4x4 array filled with 16, since every block holds 16 ones.

Constraints & Assumptions

  • The input is a numeric array of shape (16, 16) .
  • Blocks are 4x4 , do not overlap, and tile the array exactly.
  • The output has shape (4, 4) ; the input does not need to be modified.

What a Strong Answer Covers Guidance

  • A correct two-step use of np.add.reduceat , with the order of the calls and the shape of the intermediate result.
  • How reduceat turns a list of start indices into segments, including the segment that starts at the last index.
  • A check of the result against a known input.
  • The time and space cost of the vectorized approach.
  • When a reshape-based block sum is the better choice.

Follow-up Questions Guidance

  • How does reduceat define each segment, and what happens at the last index?
  • How would you generalize to arbitrary block sizes or a reshape-based approach?
  • What does your function return for an 18x18 input, and is that what a caller would expect?