Checkerboard Matrix via NumPy Strided Slicing

Quick Overview

Write a NumPy function that returns an n x n checkerboard of 0s and 1s by assigning into strided slices of a zeros array, with no Python loops. It tests index parity, slice starts and steps, integer dtypes, and edge cases such as n equal to 0 or 1.

Checkerboard Matrix via NumPy Strided Slicing

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `checkerboard(n)` so that it returns an `n x n` matrix of 0s and 1s with an integer dtype. Element `[0, 0]` is 0, and horizontally or vertically adjacent cells alternate between 0 and 1. Build the board with strided slicing on a `np.zeros` array. Do not use Python `for` or `while` loops. ```python checkerboard(4) # array([[0, 1, 0, 1], # [1, 0, 1, 0], # [0, 1, 0, 1], # [1, 0, 1, 0]]) ``` ```hint Group the cells Sort the cells into four groups by whether the row index is even or odd and whether the column index is even or odd. Which groups hold the 1s? ``` ### Constraints & Assumptions - `n >= 0`. Handle `n = 0` and `n = 1`. - The result has shape `(n, n)` and an integer dtype. ### What a Strong Answer Covers - The rule that decides whether a cell is 0 or 1, stated in terms of its row and column indices. - Slice starts and steps that select every cell that should be 1, with no gaps and no overlap. - Why the result is an integer array and why `[0, 0]` stays 0. - Behavior for `n = 0`, `n = 1` and odd `n`. - Time and space cost. ### Follow-up Questions - How would you change the function so that element `[0, 0]` is 1? - Could you build the same pattern without starting from a zeros array? - How would you extend the function to a rectangular `m x n` board?

Overview: Write a NumPy function that returns an n x n checkerboard of 0s and 1s by assigning into strided slices of a zeros array, with no Python loops. It tests index parity, slice starts and steps, integer dtypes, and edge cases such as n equal to 0 or 1.

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

Implement checkerboard(n) so that it returns an n x n matrix of 0s and 1s with an integer dtype. Element [0, 0] is 0, and horizontally or vertically adjacent cells alternate between 0 and 1.

Build the board with strided slicing on a np.zeros array. Do not use Python for or while loops.

checkerboard(4)
# array([[0, 1, 0, 1],
#        [1, 0, 1, 0],
#        [0, 1, 0, 1],
#        [1, 0, 1, 0]])

Constraints & Assumptions

  • n >= 0 . Handle n = 0 and n = 1 .
  • The result has shape (n, n) and an integer dtype.

What a Strong Answer Covers Guidance

  • The rule that decides whether a cell is 0 or 1, stated in terms of its row and column indices.
  • Slice starts and steps that select every cell that should be 1, with no gaps and no overlap.
  • Why the result is an integer array and why [0, 0] stays 0.
  • Behavior for n = 0 , n = 1 and odd n .
  • Time and space cost.

Follow-up Questions Guidance

  • How would you change the function so that element [0, 0] is 1?
  • Could you build the same pattern without starting from a zeros array?
  • How would you extend the function to a rectangular m x n board?