Zero the inner region of a square NumPy array while keeping its border

Quick Overview

Practice NumPy slicing by returning a copy of a square array with every interior element set to zero and the one-element border kept. Tests views versus copies, vectorized slice assignment, and what happens with very small arrays.

Zero the inner region of a square NumPy array while keeping its border

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a square NumPy array `a` of shape `(n, n)`, write `zero_interior(a)` that returns a copy with every interior element set to `0`. The one-element border (the first and last rows and the first and last columns) keeps its original values. The function must return a new array (use `a.copy()`) and leave the input unchanged, and it must zero the interior with a single slice assignment on the copy, without Python loops. ```python a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) zero_interior(a) # [[1, 2, 3], # [4, 0, 6], # [7, 8, 9]] ``` ```hint Views and small inputs Basic slicing returns a view, so assigning to a slice writes into the array it came from. Pick a start and stop that drop one index at each end of both axes, then check what they select when n is 1 or 2. ``` ### Constraints & Assumptions - `a` is 2-D and square, with $n \ge 1$. - For $n \le 2$ the interior is empty, so the function returns the copy unchanged. ### What a Strong Answer Covers - Copying the input first, and why a slice on its own would not protect the caller's array. - A single vectorized slice assignment that selects exactly the interior on both axes. - What that slice selects when n is 1 or 2, and whether a special case is needed. - The time and extra memory cost. ### Follow-up Questions - How would you generalize this to a border of thickness `k`? Which values of `k` need care? - Why prefer slice assignment over a boolean mask here, and when would a mask be the better choice? - How would you apply the same operation to a stack of images with shape `(batch, h, w)`, or to an array with any number of dimensions?

Overview: Practice NumPy slicing by returning a copy of a square array with every interior element set to zero and the one-element border kept. Tests views versus copies, vectorized slice assignment, and what happens with very small arrays.

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

Given a square NumPy array a of shape (n, n), write zero_interior(a) that returns a copy with every interior element set to 0. The one-element border (the first and last rows and the first and last columns) keeps its original values.

The function must return a new array (use a.copy()) and leave the input unchanged, and it must zero the interior with a single slice assignment on the copy, without Python loops.

a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

zero_interior(a)
# [[1, 2, 3],
#  [4, 0, 6],
#  [7, 8, 9]]

Constraints & Assumptions

  • a is 2-D and square, with n≥1n \ge 1 .
  • For n≤2n \le 2 the interior is empty, so the function returns the copy unchanged.

What a Strong Answer Covers Guidance

  • Copying the input first, and why a slice on its own would not protect the caller's array.
  • A single vectorized slice assignment that selects exactly the interior on both axes.
  • What that slice selects when n is 1 or 2, and whether a special case is needed.
  • The time and extra memory cost.

Follow-up Questions Guidance

  • How would you generalize this to a border of thickness k ? Which values of k need care?
  • Why prefer slice assignment over a boolean mask here, and when would a mask be the better choice?
  • How would you apply the same operation to a stack of images with shape (batch, h, w) , or to an array with any number of dimensions?