Count the 3 x 3 magic square subgrids in an integer grid

Quick Overview

Count how many contiguous 3 x 3 subgrids of an integer grid form a magic square that uses each of the numbers 1 to 9 exactly once. It tests careful window enumeration over a matrix, exact validation of rows, columns and diagonals, and handling cell values outside the allowed range.

Count the 3 x 3 magic square subgrids in an integer grid

Company: Wayfair

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, each column, and both diagonals have the same sum. Given a `row x col` grid of integers, return how many contiguous 3 x 3 subgrids are magic squares. A magic square can only contain the numbers 1 to 9, but `grid` may contain numbers up to 15. ### Function Signature ```python def num_magic_squares_inside(grid: list[list[int]]) -> int: ``` ### Rules - A subgrid is any block of 3 consecutive rows and 3 consecutive columns. Subgrids may overlap, and each top-left position is counted on its own. - A subgrid counts only when its nine cells are exactly the numbers 1 to 9, each used once, and all three rows, all three columns and both diagonals have the same sum. - If the grid has fewer than 3 rows or fewer than 3 columns, the answer is 0. ### Constraints - `row == len(grid)` - `col == len(grid[i])` - `1 <= row, col <= 10` - `0 <= grid[i][j] <= 15` ### Examples ```text Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]] Output: 1 ``` The subgrid in columns 0-2 is a magic square: its cells are 1 to 9 and every row, column and diagonal sums to 15. The subgrid in columns 1-3 also uses 1 to 9 and its rows and columns sum to 15, but its main diagonal is 3 + 1 + 2 = 6, so it does not count. ```text Input: grid = [[8]] Output: 0 ``` The grid is smaller than 3 x 3, so it has no 3 x 3 subgrid at all. ```text Input: grid = [[3,10,2],[4,5,6],[8,0,7]] Output: 0 ``` The nine values are distinct and every row, column and diagonal sums to 15, but the grid uses 0 and 10 in place of 1 and 9, so it is not a magic square under the definition.

Overview: Count how many contiguous 3 x 3 subgrids of an integer grid form a magic square that uses each of the numbers 1 to 9 exactly once. It tests careful window enumeration over a matrix, exact validation of rows, columns and diagonals, and handling cell values outside the allowed range.

Wayfair logo
Wayfair
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, each column, and both diagonals have the same sum.

Given a row x col grid of integers, return how many contiguous 3 x 3 subgrids are magic squares.

A magic square can only contain the numbers 1 to 9, but grid may contain numbers up to 15.

Function Signature

def num_magic_squares_inside(grid: list[list[int]]) -> int:

Rules

  • A subgrid is any block of 3 consecutive rows and 3 consecutive columns. Subgrids may overlap, and each top-left position is counted on its own.
  • A subgrid counts only when its nine cells are exactly the numbers 1 to 9, each used once, and all three rows, all three columns and both diagonals have the same sum.
  • If the grid has fewer than 3 rows or fewer than 3 columns, the answer is 0.

Constraints

  • row == len(grid)
  • col == len(grid[i])
  • 1 <= row, col <= 10
  • 0 <= grid[i][j] <= 15

Examples

Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
Output: 1

The subgrid in columns 0-2 is a magic square: its cells are 1 to 9 and every row, column and diagonal sums to 15. The subgrid in columns 1-3 also uses 1 to 9 and its rows and columns sum to 15, but its main diagonal is 3 + 1 + 2 = 6, so it does not count.

Input: grid = [[8]]
Output: 0

The grid is smaller than 3 x 3, so it has no 3 x 3 subgrid at all.

Input: grid = [[3,10,2],[4,5,6],[8,0,7]]
Output: 0

The nine values are distinct and every row, column and diagonal sums to 15, but the grid uses 0 and 10 in place of 1 and 9, so it is not a magic square under the definition.